//左刀右叉问题
package test;
public class KnifeFork
{
public static void main(String[] args)
{
getThread gt = new getThread();
Thread t1 = new Thread(gt, "knife");
Thread t2 = new Thread(gt, "fork");
t1.start();
t2.start();
}
}
class getThread implements Runnable
{
public String knife = "knife";
public String fork = "fork";
public boolean b = false;
public void run()
{
if(Thread.currentThread().getName().equals("knife"))
{
System.out.println(Thread.currentThread().getName() + ":knife");
synchronized (knife)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (fork)
{
System.out.println(Thread.currentThread().getName() + ":fork");
}
}
}
if(Thread.currentThread().getName().equals("fork"))
{
System.out.println(Thread.currentThread().getName() + ":fork");
synchronized (fork)
{
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (knife)
{
System.out.println(Thread.currentThread().getName() + ":knife");
}
}
}
}
}