package com.itheima;
//死锁问题
public class Thread_sisuo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t1=new Thread(new Test_2(true));
Thread t2=new Thread(new Test_2(false));
//Thread t3=new Thread(new Test_2(true));
//Thread t4=new Thread(new Test_2(false));
t1.start();
t2.start();
//t3.start();
//t4.start();
}
}
class Test_2 implements Runnable
{
private boolean flog;
public Test_2(boolean flog){
this.flog=flog;
}
public void run(){
if(flog){
while(true)
{
synchronized(MyLock.locka)
{
System.out.println("if locka");
synchronized(MyLock.lockb)
{
System.out.println("if lockb");
}
}
}
}
else
{
while(true)
{
synchronized(MyLock.lockb)
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
}
}
}
}
}
class MyLock
{
static Object locka=new Object();
static Object lockb=new Object();
}
上面是跟着老毕写的代码,请各位大神给说说下面这段代码中线程进入到第一个代码块后具体做了什么行不?这里真心不懂。(最重要的是怎么判断锁的)
synchronized(MyLock.lockb)
{
System.out.println("else lockb");
synchronized(MyLock.locka)
{
System.out.println("else locka");
}
} |