JAVA多线程 JOIN
对于Java开发人员,多线程应该是必须熟练应用的知识点,特别是开发基于Java语言的产品。本文将深入浅出的表述Java多线程的知识点,在后续的系列里将侧重于Java5由Doug Lea教授提供的Concurrent并行包的设计思想以及具体实现与应用。
如何才能深入浅出呢,我的理解是带着问题,而不是泛泛的看。所以该系列基本以解决问题为主,当然我也非常希望读者能够提出更好的解决问题的方案以及提出更多的问题。由于水平有限,如果有什么错误之处,请大家提出,共同讨论,总之,我希望通过该系列我们能够深入理解Java多线程来解决我们实际开发的问题。
作为开发人员,我想没有必要讨论多线程的基础知识,比如什么是线程? 如何创建等 ,这些知识点是可以通过书本和Google获得的。本系列主要是如何理深入解多线程来帮助我们平时的开发,比如线程池如何实现? 如何应用锁等。
(1)方法Join是干啥用的? 简单回答,同步,如何同步? 怎么实现的? 下面将逐个回答。
自从接触Java多线程,一直对Join理解不了。JDK是这样说的:
1
2
3
|
join
public final void join( long millis) throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.
|
大家能理解吗? 字面意思是等待一段时间直到这个线程死亡,我的疑问是那个线程,是它本身的线程还是调用它的线程的,上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread( new RunnableImpl());
t.start();
try {
t.join( 1000 );
System.out.println( "joinFinish" );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println( "Begin sleep" );
Thread.sleep( 1000 );
System.out.println( "End sleep" );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
|
结果是:
1
2
3
|
Begin sleep
End sleep
joinFinish
|
明白了吧,当main线程调用t.join时,main线程等待t线程,等待时间是1000,如果t线程Sleep 2000呢
1
2
3
4
5
6
7
8
9
10
11
|
public void run() {
try {
System.out.println( "Begin sleep" );
// Thread.sleep(1000);
Thread.sleep( 2000 );
System.out.println( "End sleep" );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
|
结果是:
1
2
3
|
Begin sleep
joinFinish
End sleep
|
也就是说main线程只等1000毫秒,不管T什么时候结束,如果是t.join()呢, 看代码:
1
2
3
|
public final void join() throws InterruptedException {
join( 0 );
}
|
就是说如果是t.join() = t.join(0) 0 JDK这样说的 A timeout of 0 means to wait forever 字面意思是永远等待,是这样吗?
其实是等到t结束后。
这个是怎么实现的吗? 看JDK代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
/**
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @param millis the time to wait in milliseconds.
* @exception InterruptedException if any thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
*/
public final synchronized void join( long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0 ;
if (millis < 0 ) {
throw new IllegalArgumentException( "timeout value is negative" );
}
if (millis == 0 ) {
while (isAlive()) {
wait( 0 );
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0 ) {
break ;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
|
其实Join方法实现是通过wait(小提示:Object 提供的方法)。 当main线程调用t.join时候,main线程会获得线程对象t的锁(wait 意味着拿到该对象的锁),调用该对象的wait(等待时间),直到该对象唤醒main线程,比如退出后。
这就意味着main 线程调用t.join时,必须能够拿到线程t对象的锁,如果拿不到它是无法wait的,刚开的例子t.join(1000)不是说明了main线程等待1秒,如果在它等待之前,其他线程获取了t对象的锁,它等待时间可不就是1毫秒了。上代码介绍:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread( new RunnableImpl());
new ThreadTest(t).start();
t.start();
try {
t.join();
System.out.println( "joinFinish" );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ThreadTest extends Thread {
Thread thread;
public ThreadTest(Thread thread) {
this .thread = thread;
}
@Override
public void run() {
holdThreadLock();
}
public void holdThreadLock() {
synchronized (thread) {
System.out.println( "getObjectLock" );
try {
Thread.sleep( 9000 );
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println( "ReleaseObjectLock" );
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println( "Begin sleep" );
Thread.sleep( 2000 );
System.out.println( "End sleep" );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
|
在main方法中 通过new ThreadTest(t).start();实例化ThreadTest 线程对象, 它在holdThreadLock()方法中,通过synchronized (thread),获取线程对象t的锁,并Sleep(9000)后释放,这就意味着,即使main方法t.join(1000),等待一秒钟,它必须等待ThreadTest 线程释放t锁后才能进入wait方法中,它实际等待时间是9000+1000 MS
运行结果是:
1
2
3
4
5
|
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish
|
相关文章
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-25 49
-
2025-05-25 31
-
详解Spring Cloud Stream使用延迟消息实现定时任务(RabbitMQ)
2025-05-29 43 -
使用自助建站微信小程序时,怎样选择合适的模板以匹配品牌形象?
2025-06-04 78 -
2025-05-29 88