java线程锁问题,求各位帮帮忙
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jointest;
/**
*
* @author Admin
*/
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();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jointest;
/**
*
* @author Admin
*/
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");
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jointest;
/**
*
* @author Admin
*/
class RunnableImpl implements Runnable {
@override
public synchronized void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(2000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
运行输出的一种结果是这样的
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish
为什么在new ThreadTest(t).start()这个线程上加了锁,new ThreadTest(t).start()这个线程还没有结束,线程t.start()就开始运行了?