/*
- join线程,调用线程阻塞
*/
package com.fatiaoyu;
public class Join_test {
public static void main(String[] args) throws Exception {
for(int i = 0; i<100; i++) {
System.out.println(Thread.currentThread().getName()+",i="+i);
if(i==20) {
MyThread4 mt = new MyThread4();
Thread t = new Thread(mt);
t.setName("线程666666");
t.start();
t.join();
}
}
}
}
class MyThread4 implements Runnable{
public void run() {
for(int i =0;i<10;i++) {
System.out.println(Thread.currentThread().getName()+",i="+i);
}
}
}
打印结果:
package com.fatiaoyu;
public class join_test5 {
public static void main(String[] args) {
MyThread5 mt = new MyThread5();
Thread thread = new Thread(mt);
thread.setDaemon(true); //设置后台线程
thread.setName("我是一个后台线程");
thread.start();
for(int i =0;i<100;i++) {
System.out.println(Thread.currentThread().getName()+",i="+i);
}
}
}
class MyThread5 implements Runnable{
public void run() {
for(int i=0;i<1000;i++) {
System.out.println(Thread.currentThread().getName()+",i="+i);
}
}
}
打印结果: