想要死锁发生,应该是这样的。我给你该好了class ObjectA {
public synchronized void foo(ObjectB b) {
String name = Thread.currentThread ().getName ();
System.out.println(name + " entering the ObjectA's foo");
try {
Thread.sleep (1000);
} catch(InterruptedException e) {
System.out.println("InterruptedException caught!");
}
System.out.println(name + " prepearing to enter the ObjectB's last");
b.last();
}
public synchronized void last() {
System.out.println("Now entering the ObjectA's last");
}
}
class ObjectB {
public synchronized void bar(ObjectA a) {
String name = Thread.currentThread ().getName ();
System.out.println(name + " entering the ObjectB's bar");
try {
Thread.sleep (1000);
} catch(InterruptedException e) {
System.out.println("InterruptedException caught!");
}
System.out.println(name + " prepearing to enter the ObjectA's last");
a.last ();
}
public synchronized void last() {
System.out.println("Now entering the ObjectB's last");
}
}
class ThreadOne implements Runnable {
Thread t;
ObjectA a ;
ObjectB b ;
ThreadOne(ObjectA a,ObjectB b) {
this.a=a;
this.b=b;
t = new Thread(this, "ThreadOne");
t.start ();
}
public void run() {
a.foo (b);
}
}
class ThreadTwo implements Runnable {
Thread t;
ObjectA a;
ObjectB b;
ThreadTwo(ObjectA a,ObjectB b) {
this.a=a;
this.b=b;
t = new Thread(this, "ThreadTwo");
t.start ();
}
public void run() {
b.bar (a);
}
}
class DeadLock{
public static void main(String[] args)
{
ObjectA a = new ObjectA();
ObjectB b = new ObjectB();
new ThreadOne(a,b);
new ThreadTwo(a,b);
System.out.println("ni hai zai wo de huai bao");
}
}
public synchronized void foo(ObjectB b) {
String name = Thread.currentThread ().getName ();
System.out.println(name + " entering the ObjectA's foo");
try {
Thread.sleep (1000);
} catch(InterruptedException e) {
System.out.println("InterruptedException caught!");
}
System.out.println(name + " prepearing to enter the ObjectB's last");
b.last();
}
public synchronized void last() {
System.out.println("Now entering the ObjectA's last");
}
}
class ObjectB {
public synchronized void bar(ObjectA a) {
String name = Thread.currentThread ().getName ();
System.out.println(name + " entering the ObjectB's bar");
try {
Thread.sleep (1000);
} catch(InterruptedException e) {
System.out.println("InterruptedException caught!");
}
System.out.println(name + " prepearing to enter the ObjectA's last");
a.last ();
}
public synchronized void last() {
System.out.println("Now entering the ObjectB's last");
}
}
class ThreadOne implements Runnable {
Thread t;
ObjectA a ;
ObjectB b ;
ThreadOne(ObjectA a,ObjectB b) {
this.a=a;
this.b=b;
t = new Thread(this, "ThreadOne");
t.start ();
}
public void run() {
a.foo (b);
}
}
class ThreadTwo implements Runnable {
Thread t;
ObjectA a;
ObjectB b;
ThreadTwo(ObjectA a,ObjectB b) {
this.a=a;
this.b=b;
t = new Thread(this, "ThreadTwo");
t.start ();
}
public void run() {
b.bar (a);
}
}
class DeadLock{
public static void main(String[] args)
{
ObjectA a = new ObjectA();
ObjectB b = new ObjectB();
new ThreadOne(a,b);
new ThreadTwo(a,b);
System.out.println("ni hai zai wo de huai bao");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货