多线程程序运行有点问题,请朋友们帮忙,谢谢!
运行老是不完整,不知道是不是在哪里死锁了?请大家帮忙看看。
public class ThreadTest2 {
public volatile static int a1 = 0;
public volatile static int a2 = 0;
static Object obj = new Object();
public static void main(String[] args) {
Thread11 t1 = new Thread11();
Thread22 t2 = new Thread22();
t1.start();
t2.start();
}
}
class Thread11 extends Thread{
public void run(){
System.out.println("thread1 enter");
for (int i = 0; i < 100; i++) {
if (ThreadTest2.a1-ThreadTest2.a2>5) {
try {
synchronized (ThreadTest2.obj) {
ThreadTest2.obj.wait();
ThreadTest2.obj.notifyAll();
}
} catch (InterruptedException e) {}
}
++ThreadTest2.a1 ;
System.out.println("a1---"+ThreadTest2.a1);
}
}
}
class Thread22 extends Thread{
public void run(){
for (int i = 0; i < 100; i++) {
if (ThreadTest2.a2-ThreadTest2.a1>5) {
try {
synchronized (ThreadTest2.obj) {
ThreadTest2.obj.notifyAll();
ThreadTest2.obj.wait();
}
} catch (InterruptedException e) {}
}
++ThreadTest2.a2;
System.out.println("a2---"+ThreadTest2.a2);
}
}
}