介绍java多线程例子
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
int i=0;
while(true) {
System.out.println("main():
"+Thread.currentThread().getName() + " is running");
if(i++ ==10000) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread:
"+Thread.currentThread().getName() + " is running");
}
}
}
当main线程中工作10000下后,把线程TestThread 联合进来,实际上就是main线程运行10000下后,一直等到TestThread 完成后,再运行。
c,联合10秒和分开:
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
int i=0;
while(true) {
System.out.println("main():
"+Thread.currentThread().getName() + " is running");
if(i++ ==10000) {
try {