Join 方法:本质上还是根据wait方法实现的。分析join源码发现join方法本身是使用了synchronized修饰符的。是加在方法上面的,意味着。 获取了当前对象的锁,然后继续发现里面的代码调用了wait。意味着我们先锁,再释放,等待唤醒,什么情况下被唤醒呢:
/**
* Waits at most {@code millis} milliseconds for this thread to
* die. A timeout of {@code 0} means to wait forever.
*
* <p> This implementation uses a loop of {@code this.wait} calls
* conditioned on {@code this.isAlive}. As a thread terminates the
* {@code this.notifyAll} method is invoked. It is recommended that
* applications not use {@code wait}, {@code notify}, or
* {@code notifyAll} on {@code Thread} instances.
*
* @param millis
* the time to wait in milliseconds
*
* @throws IllegalArgumentException
* if the value of {@code millis} is negative
*
* @throws 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(0),让程序等待,那么什么时间锁被释放呢?。因为是以线程对象作为锁的:
当线程运行结束的时候,notify是被线程的子系统调用的
/**
* This method is called by the system to give a Thread
* a chance to clean up before it actually exits.
*/
private void exit() {
if (group != null) {
group.threadTerminated(this);
group = null;
}
/* Aggressively null out all reference fields: see bug 4006245 */
target = null;
/* Speed the release of some of these resources */
threadLocals = null;
inheritableThreadLocals = null;
inheritedAccessControlContext = null;
blocker = null;
uncaughtExceptionHandler = null;
}
/**
* Notifies the group that the thread {@code t} has terminated.
*
* <p> Destroy the group if all of the following conditions are
* true: this is a daemon thread group; there are no more alive
* or unstarted threads in the group; there are no subgroups in
* this thread group.
*
* @param t
* the Thread that has terminated
*/
void threadTerminated(Thread t) {
synchronized (this) {
remove(t);
if (nthreads == 0) {
notifyAll();
}
if (daemon && (nthreads == 0) &&
(nUnstartedThreads == 0) && (ngroups == 0))
{
destroy();
}
}
}
分别以普通对象和线程对象作为锁,当使用线程对象作为所得时候,如果锁对象执行完毕了。wait就会停止等待继续执行
public class ThreadTestl {
public static void main(String[] args) {
final MyLockThread o = new MyLockThread();
//final Object oo = new Object();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (o) {
System.err.println(1);
try {
o.wait(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(2);
}
}
}).start();
o.start();
}
static class MyLockThread extends Thread {
public MyLockThread() {
}
@Override
public void run() {
System.err.println("MyLockThread");
}
}
}