介绍java多线程例子
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread:
"+Thread.currentThread().getName() + " is running");
}
}
}
运行当前台线程main退出后自动退出。去掉setDeamon,TestThread 就不会退出 Java 多线程例子3 联合线程 join()
a,直接联合:
public class ThreadDemo {
public static void main(String[] args) {
Thread t = new TestThread();
// t.setDaemon(true);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true) {
System.out.println("main():
"+Thread.currentThread().getName() + " is running");
}
}
}
class TestThread extends Thread {
public void run() {
while(true) {
System.out.println("TestThread:
"+Thread.currentThread().getName() + " is running");
}
}
}
这种直接联合,2个线程就完全成了一个线程了。
b,条件联合: