- 浏览: 102594 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (99)
- 经济 (1)
- dwr (2)
- 测试 (0)
- java (29)
- resin (1)
- oracle (3)
- 感悟 (1)
- jvm (15)
- mina2 (5)
- j2se (12)
- linux (28)
- protobuf (1)
- tcp/ip (0)
- jdbc (0)
- 数据库 (4)
- 游戏 (0)
- 技术文档 (1)
- nosql (2)
- 算法 (2)
- apache (2)
- mysql (1)
- hashcode (1)
- spring (2)
- quartz (5)
- netcat (2)
- 分页 (1)
- 正则 (0)
- shell (1)
- lsof (1)
- nginx (1)
- git (1)
最新评论
-
fys124974704:
你试下将第三条写成以下这样,你会发现你的结论不对:select ...
ORACLE分页SQL语句 -
ikon:
两个乘数没有转成integer,而是当成字符串;BigInte ...
计算任意2个正整数的乘积 -
kidding87:
效率不是很高,思路没有问题,但是你的两个乘数输入都都转为Int ...
计算任意2个正整数的乘积 -
k1280000:
------------------------同意!
学习之道
一、在研究join的用法之前,先明确两件事情。
1.join方法定义在Thread类中,则调用者必须是一个线程,
例如:
Thread t = new CustomThread();//这里一般是自定义的线程类
t.start();//线程起动
t.join();//此处会抛出InterruptedException异常
2.上面的两行代码也是在一个线程里面执行的。
以上出现了两个线程,一个是我们自定义的线程类,我们实现了run方法,做一些我们需要的工作;另外一个线程,生成我们自定义线程类的对象,然后执行
customThread.start();
customThread.join();
在这种情况下,两个线程的关系是一个线程由另外一个线程生成并起动,所以我们暂且认为第一个线程叫做“子线程”,另外一个线程叫做“主线程”。
二、为什么要用join()方法
主线程生成并起动了子线程,而子线程里要进行大量的耗时的运算(这里可以借鉴下线程的作用),当主线程处理完其他的事务后,需要用到子线程的处理结果,这个时候就要用到join();方法了。
三、join方法的作用
在网上看到有人说“将两个线程合并”。这样解释我觉得理解起来还更麻烦。不如就借鉴下API里的说法:
“等待该线程终止。”
解释一下,是主线程(我在“一”里已经命名过了)等待子线程的终止。也就是在子线程调用了join()方法后面的代码,只有等到子线程结束了才能执行。(Waits for this thread to die.)
四、用实例来理解
写一个简单的例子来看一下join()的用法,一共三个类:
1.CustomThread 类
2. CustomThread1类
3. JoinTestDemo 类,main方法所在的类。
代码1:
2/** *//**
3 *
4 * @author bzwm
5 *
6 */
7class CustomThread1 extends Thread {
8 public CustomThread1() {
9 super("[CustomThread1] Thread");
10 };
11 public void run() {
12 String threadName = Thread.currentThread().getName();
13 System.out.println(threadName + " start.");
14 try {
15 for (int i = 0; i < 5; i++) {
16 System.out.println(threadName + " loop at " + i);
17 Thread.sleep(1000);
18 }
19 System.out.println(threadName + " end.");
20 } catch (Exception e) {
21 System.out.println("Exception from " + threadName + ".run");
22 }
23 }
24}
25class CustomThread extends Thread {
26 CustomThread1 t1;
27 public CustomThread(CustomThread1 t1) {
28 super("[CustomThread] Thread");
29 this.t1 = t1;
30 }
31 public void run() {
32 String threadName = Thread.currentThread().getName();
33 System.out.println(threadName + " start.");
34 try {
35 t1.join();
36 System.out.println(threadName + " end.");
37 } catch (Exception e) {
38 System.out.println("Exception from " + threadName + ".run");
39 }
40 }
41}
42public class JoinTestDemo {
43 public static void main(String[] args) {
44 String threadName = Thread.currentThread().getName();
45 System.out.println(threadName + " start.");
46 CustomThread1 t1 = new CustomThread1();
47 CustomThread t = new CustomThread(t1);
48 try {
49 t1.start();
50 Thread.sleep(2000);
51 t.start();
52 t.join();//在代碼2里,將此處注釋掉
53 } catch (Exception e) {
54 System.out.println("Exception from main");
55 }
56 System.out.println(threadName + " end!");
57 }
58}
打印结果:
main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。
[CustomThread1] Thread start.//线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。
修改一下代码,得到代码2:(这里只写出修改的部分)
2 public static void main(String[] args) {
3 String threadName = Thread.currentThread().getName();
4 System.out.println(threadName + " start.");
5 CustomThread1 t1 = new CustomThread1();
6 CustomThread t = new CustomThread(t1);
7 try {
8 t1.start();
9 Thread.sleep(2000);
10 t.start();
11// t.join();//在代碼2里,將此處注釋掉
12 } catch (Exception e) {
13 System.out.println("Exception from main");
14 }
15 System.out.println(threadName + " end!");
16 }
17}
打印结果:
main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);
[CustomThread1] Thread start. //线程CustomThread1起动
[CustomThread1] Thread loop at 0//线程CustomThread1执行
[CustomThread1] Thread loop at 1//线程CustomThread1执行
main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。
[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。
[CustomThread1] Thread loop at 2//线程CustomThread1继续执行
[CustomThread1] Thread loop at 3//线程CustomThread1继续执行
[CustomThread1] Thread loop at 4//线程CustomThread1继续执行
[CustomThread1] Thread end. //线程CustomThread1结束了
[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果
五、从源码看join()方法
在CustomThread的run方法里,执行了t1.join();,进入看一下它的JDK源码:
2join(0);
3}
然后进入join(0)方法:
2 * Waits at most <code>millis</code> milliseconds for this thread to
3 * die. A timeout of <code>0</code> means to wait forever. //注意这句
4 *
5 * @param millis the time to wait in milliseconds.
6 * @exception InterruptedException if another thread has interrupted
7 * the current thread. The <i>interrupted status</i> of the
8 * current thread is cleared when this exception is thrown.
9 */
10 public final synchronized void join(long millis) //参数millis为0.
11 throws InterruptedException {
12long base = System.currentTimeMillis();
13long now = 0;
14if (millis < 0) {
15 throw new IllegalArgumentException("timeout value is negative");
16}
17if (millis == 0) {//进入这个分支
18 while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1.
19 wait(0);//阻塞
20 }
21} else {
22 while (isAlive()) {
23 long delay = millis - now;
24 if (delay <= 0) {
25 break;
26 }
27 wait(delay);
28 now = System.currentTimeMillis() - base;
29 }
30}
31 }
单纯从代码上看,如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。
发表评论
-
Quartz定时任务学习(一)简单任务
2012-04-27 12:30 843学习quartz首先了解三个概念: 调度器:负责调度作 ... -
介绍Quartz
2012-04-27 12:22 1411介绍Quartz Quartz是一个开源的任务调度 ... -
理解Quartz触发器(1)
2012-04-27 11:37 866Quartz中一个Job往往是 ... -
Quartz中SimpleTrigger的探讨
2012-04-27 11:34 7041.来写一个每隔10秒启动一次任务的例子. import j ... -
关于hashcode 里面 使用31 系数的问题
2012-03-27 09:16 809首先我们来了解一下h ... -
理解Java对象序列化
2012-02-15 09:38 696关于Java序列化的文章早 ... -
认识Arrays(一)打印
2012-02-03 16:52 571Arrays提供了一组操作array的静态方法。 一、基本类 ... -
apache.commons.io 笔记1
2012-01-19 17:13 1064看看,常见的东西都有了,如查询盘的剩余空间,文件夹大小, ... -
commons-io 自动加载配置文件
2012-01-19 16:55 747org.apache.commons.io.monitor.F ... -
oracle java数据类型对应表
2011-12-05 13:37 0[img]http://dl.iteye.com/upload ... -
Iterator和Enumeration的主要区别
2011-11-25 14:38 853(1)java中的集合类都提供了返回Iterator的方法 ... -
计算任意2个正整数的乘积
2011-11-12 21:36 1205接上一篇整数的阶乘算法,来计算2个正整数的乘积; 算法如下: ... -
计算任意正整数的阶乘
2011-11-12 20:17 667由于阶乘的结果会超出java数据类型的最大范围,所 ... -
Java调用外部程序技巧
2011-11-08 09:17 796前些天使用Java调用外部程序的时候,发现线程会堵塞在wa ... -
多核平台下的JAVA优化
2011-11-08 09:12 632现在多核CPU是主流。利用多核技术,可以有效发挥硬件的能力 ... -
使用 Eclipse Memory Analyzer 进行堆转储文件分析
2011-11-07 17:09 772http://www.ibm.com/developerwor ... -
infoQ 文档ppt
2011-11-07 11:43 1251http://www.docin.com/app/user/c ... -
游戏性能监控
2011-11-07 10:18 01、cpu消耗(耗时)监控 在功能代码前后取时间值,相减,发 ... -
使用Cacti监控你的网络(一)- Cacti概述及工作流程
2011-11-07 09:36 675http://blog.sina.com.cn/s/b ... -
Java 理论与实践: 应用 fork-join 框架,第 2 部分
2011-10-21 10:40 726在 上一期 Java 理论与实践 中,我们研究了 fork ...
相关推荐
7、 浅析 Java Thread.join() : java多线程实现主线程等待所有子线程执行完毕 16 8、 线程运行中抛出异常的处理 19 9、 Callable 有返回值的线程 20 10、 Callable结合FutureTask的多线程使用(免打扰模式) 24
浅析java.math.BigInteger构造过程.pdf
浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt浅析STP协议.ppt
浅析Java企业开发.pdf
浅析JAVA代理模式.pdf
供应链金融风险管控的浅析贸易金融.docx供应链金融风险管控的浅析贸易金融.docx供应链金融风险管控的浅析贸易金融.docx供应链金融风险管控的浅析贸易金融.docx供应链金融风险管控的浅析贸易金融.docx供应链金融风险...
浅析JAVA多线程.pdf
2022年浅析Java中Data类的应用Java教程.docx
浅析云计算安全.pdf
浅析Ajax技术.pdf
Java ClassName.this中类名.this关键字 的理解大家都了解多少,有不太了解的朋友可以参考下本文一起学习学习
LTE小区重选准则浅析-v..doc
浅析网页设计.pdf
浅析电子商务.doc
云计算安全防护体系构建浅析4100字.docx云计算安全防护体系构建浅析4100字.docx
云计算安全防护体系构建浅析4100字.pdf云计算安全防护体系构建浅析4100字.pdf
浅析网络水军.doc
浅析小波变换.pptx