Java线程的join()方法源码问题,锁的问题

yq__in 2017-04-25 11:34:53
    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); //wait不应该是this.wait()吗,如果主线程中有t.join(),那么这里不应该是t.wait()吗?
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}

上面是join方法的源码,下面是个例子,如果说在主线程中创建了线程t,启动后主线程调用了t.join()方法,join()方法中的wait()应该是this.wait()吗,那么传入join()方法的this不应该是t吗,所以调用的wait()为什么不是t的,而是调用的主线程wait()方法,让主线程wait()???
public class JoinDemo {
private static int a = 0;

public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int k = 0; k < 5; k++) {
a = a + 1;
}
}
});
t.start();
t.join();
System.out.println(a);
}
}
...全文
473 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yq__in 2017-05-01
  • 打赏
  • 举报
回复
引用 7 楼 abcdefghiijklmnopqrs 的回复:
[quote=引用 6 楼 yq__in 的回复:] [quote=引用 5 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 4 楼 yq__in 的回复:] [quote=引用 3 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 2 楼 yq__in 的回复:] [quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.[/quote] 不好意思,我还是没太看明白就是说在main中的Thread t对象的方法调用都可以看成是main线程在调用这个方法,这样理解对吗?那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。感觉我不是很理解。。。还是说是只是wait()方法比较特殊,线程对象t 在main中调用t.wait()就相当于主线程调用普通方法???麻烦您再解释一下吧[/quote] 那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。 这有什么不对的,这些方法明明就是在主线程里面调用的啊,把线程对象当Object就行了[/quote] 按照”主线程调用t.wait()就是主线程执行wait()”这种思路,那么主线程调用t.start()、t.join()也是主线程执行start()、join(),这样明显就不对了呀,这两个方法应当还是线程t执行 的不是吗[/quote] 都是主线程调用的,建议你多看看线程相关的书, 这是对start方法的解释 主线程调用start方法,start方法中调用native的start0方法,start0方法调用JVM_StartThread方法, JVM_StartThread方法中有这样的调用: .......... native_thread = new JavaThread(&thread_entry, sz); ........... Thread::start(native_thread); 这次是真正的开启了一个线程. 新开启的线程执行以下方法: static void thread_entry(JavaThread* thread, TRAPS) { HandleMark hm(THREAD); Handle obj(THREAD, thread->threadObj()); JavaValue result(T_VOID); JavaCalls::call_virtual(&result,obj, KlassHandle(THREAD,SystemDictionary::Thread_klass()), vmSymbolHandles::run_method_name(), //这里执行的是线程中重写的run方法 vmSymbolHandles::void_method_signature(),THREAD); } 这是对join方法的解释: private static int a = 0; public static void main(String[] args) throws InterruptedException { final Object o = new Object(); Thread t = new Thread(new Runnable() { @Override public void run() { for (int k = 0; k < 100000; k++) { a = a + 1; } synchronized (o) { o.notify(); } } }); t.start(); synchronized (o) { o.wait(0); } System.out.println(a); } 你给出的方法和这个方法是等价的,只不过是没有用线程对象的join方法,但是两个的思想和运行结果完全是等价的. 那为什么要用t.join呢,因为jdk帮你封装了我给出的代码,简化了编程,线程终结时会调用t.notify而不用你自己像我那样手动的唤醒线程了[/quote] 线程这方面的知识我得好好补补了,谢谢了
yq__in 2017-04-28
  • 打赏
  • 举报
回复
引用 5 楼 abcdefghiijklmnopqrs 的回复:
[quote=引用 4 楼 yq__in 的回复:] [quote=引用 3 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 2 楼 yq__in 的回复:] [quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.[/quote] 不好意思,我还是没太看明白就是说在main中的Thread t对象的方法调用都可以看成是main线程在调用这个方法,这样理解对吗?那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。感觉我不是很理解。。。还是说是只是wait()方法比较特殊,线程对象t 在main中调用t.wait()就相当于主线程调用普通方法???麻烦您再解释一下吧[/quote] 那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。 这有什么不对的,这些方法明明就是在主线程里面调用的啊,把线程对象当Object就行了[/quote] 按照”主线程调用t.wait()就是主线程执行wait()”这种思路,那么主线程调用t.start()、t.join()也是主线程执行start()、join(),这样明显就不对了呀,这两个方法应当还是线程t执行 的不是吗
  • 打赏
  • 举报
回复
引用 4 楼 yq__in 的回复:
[quote=引用 3 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 2 楼 yq__in 的回复:] [quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.[/quote] 不好意思,我还是没太看明白就是说在main中的Thread t对象的方法调用都可以看成是main线程在调用这个方法,这样理解对吗?那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。感觉我不是很理解。。。还是说是只是wait()方法比较特殊,线程对象t 在main中调用t.wait()就相当于主线程调用普通方法???麻烦您再解释一下吧[/quote] 那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。 这有什么不对的,这些方法明明就是在主线程里面调用的啊,把线程对象当Object就行了
  • 打赏
  • 举报
回复
引用 6 楼 yq__in 的回复:
[quote=引用 5 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 4 楼 yq__in 的回复:] [quote=引用 3 楼 abcdefghiijklmnopqrs 的回复:] [quote=引用 2 楼 yq__in 的回复:] [quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.[/quote] 不好意思,我还是没太看明白就是说在main中的Thread t对象的方法调用都可以看成是main线程在调用这个方法,这样理解对吗?那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。感觉我不是很理解。。。还是说是只是wait()方法比较特殊,线程对象t 在main中调用t.wait()就相当于主线程调用普通方法???麻烦您再解释一下吧[/quote] 那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。 这有什么不对的,这些方法明明就是在主线程里面调用的啊,把线程对象当Object就行了[/quote] 按照”主线程调用t.wait()就是主线程执行wait()”这种思路,那么主线程调用t.start()、t.join()也是主线程执行start()、join(),这样明显就不对了呀,这两个方法应当还是线程t执行 的不是吗[/quote] 都是主线程调用的,建议你多看看线程相关的书, 这是对start方法的解释 主线程调用start方法,start方法中调用native的start0方法,start0方法调用JVM_StartThread方法, JVM_StartThread方法中有这样的调用: .......... native_thread = new JavaThread(&thread_entry, sz); ........... Thread::start(native_thread); 这次是真正的开启了一个线程. 新开启的线程执行以下方法: static void thread_entry(JavaThread* thread, TRAPS) { HandleMark hm(THREAD); Handle obj(THREAD, thread->threadObj()); JavaValue result(T_VOID); JavaCalls::call_virtual(&result,obj, KlassHandle(THREAD,SystemDictionary::Thread_klass()), vmSymbolHandles::run_method_name(), //这里执行的是线程中重写的run方法 vmSymbolHandles::void_method_signature(),THREAD); } 这是对join方法的解释: private static int a = 0; public static void main(String[] args) throws InterruptedException { final Object o = new Object(); Thread t = new Thread(new Runnable() { @Override public void run() { for (int k = 0; k < 100000; k++) { a = a + 1; } synchronized (o) { o.notify(); } } }); t.start(); synchronized (o) { o.wait(0); } System.out.println(a); } 你给出的方法和这个方法是等价的,只不过是没有用线程对象的join方法,但是两个的思想和运行结果完全是等价的. 那为什么要用t.join呢,因为jdk帮你封装了我给出的代码,简化了编程,线程终结时会调用t.notify而不用你自己像我那样手动的唤醒线程了
yq__in 2017-04-27
  • 打赏
  • 举报
回复
引用 3 楼 abcdefghiijklmnopqrs 的回复:
[quote=引用 2 楼 yq__in 的回复:] [quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.[/quote] 不好意思,我还是没太看明白就是说在main中的Thread t对象的方法调用都可以看成是main线程在调用这个方法,这样理解对吗?那么如果main中有t.start()、t.join()、t.wait()都看成是主线程在调用这些方法,这样就不对了。。。感觉我不是很理解。。。还是说是只是wait()方法比较特殊,线程对象t 在main中调用t.wait()就相当于主线程调用普通方法???麻烦您再解释一下吧
  • 打赏
  • 举报
回复
引用 2 楼 yq__in 的回复:
[quote=引用 1 楼 abcdefghiijklmnopqrs 的回复:] JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?[/quote] wait是Object类的方法,把对象t当成是一个普通的对象,你这样想,在主线程中这样调用 public static void main(String[] args) throws InterruptedException { Object o = new Object(); synchronized (o) { o.wait(0); } } 调用wait方法是让调用方法的线程挂起,和调用的是哪个对象上的wait方法没关系,把Thread对象当作一个普通对象就行了.
yq__in 2017-04-27
  • 打赏
  • 举报
回复
引用 1 楼 abcdefghiijklmnopqrs 的回复:
JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行
您好,我还有一点不太明白,主线程调用这样一个过程:t.join-->t.wait(0)->...,其实表示的是主线程执行了wait()方法,并不是t.wait()就代表Thread t 执行wait()方法,是这样理解吗?总感觉wait()方法是由Thread t调用的,毕竟传给他的this是Thread t,但是实际调用就成了主线程在调用? 那detachThread方法中会调用对象t的notifyAll方法,这里应当是Thread t来执行t.notifyAll()的吧?
  • 打赏
  • 举报
回复
JoinDemo执行到15行t.join();时(这个方法在主线程中执行,只是一个普通的方法调用而已), Thread.join方法中,执行到第13行wait(0);调用this.wait(0)方法,其中this为Thread t对象, wait方法的说明: Causes the current thread to wait until another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or * some other thread interrupts the current thread, or a certain * amount of real time has elapsed. 执行这个方法的线程会等待直到上述的某一个事件发生,而执行这个方法的线程就是主线程啊, 主线程中的方法调用: t.join-->t.wait(0)->... 线程t中要执行的代码为: threadStart() => attachThread() -> 执行Thread的run() -> detachThread() 而detachThread方法中会调用对象t的notifyAll方法, 主线程被唤醒继续执行

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧