您的位置:首页 > 其它

Thread.join()用法

2017-04-24 18:08 274 查看
示例:

package com.threadlocal的使用;

public class ThreadLocalTest {

//ThreadLocal<String> tl = new ThreadLocal<String>();

private static int a=0;

private synchronized static void incr() {

for (int i=0; i<5; i++){
try {
Thread.sleep(1000);
a++;
System.out.println(a);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

}

public static void main(String[] args) throws InterruptedException {

Thread t1 = new Thread(new Runnable() {

public void run() {
incr();
}
},"t1");
t1.start();
//主线程阻塞,一直等到t1线程运行结束后,主线程继续运行
t1.join();

System.out.println(a);
}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: