62,625
社区成员
发帖
与我相关
我的任务
分享//测试类,不必多说
public class TestDeadLock implements Runnable{
public static void main(String[] args) {
//创建并启动新线程,不废话
TestDeadLock test = new TestDeadLock();
Thread tt = new Thread(test);
tt.start();
synchronized("lock"){
try {
//调用jion()方法,等待tt线程结束
tt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-------main方法被执行了--------");
}
}
//run方法,不必多说
@Override
public void run() {
try {
//等待一下,确保main方法先拿到锁
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized("lock"){
System.out.println("-------run方法被执行了--------");
}
}
}
class DeadLock extends Thread
{
public static void main(String[] args )
{
DeadLock t1 = new DeadLock();
t1.start();
t1.join1();
System.out.println("main_over");
}
public void run()
{
System.out.println("run over");
}
public synchronized void join1()
{
try{this.wait();}catch(InterruptedException e){}
}
public synchronized void start()
{
super.start();
this.notify();
System.out.println("startover");
}
}