Java多線程的交替執行

      讀完Think In Java的多線程,深有感悟,花了1個小時,寫了一個多線程交替執行程序,大家可以參考,如有好的意見,請提出,謝謝!

package com.thread;


public class ThreadTest implements Runnable {



public void run() {
int j = 0;
while (true) {

try {
synchronized (this) {
if (j == 5) {
j = 0;
Tmp.getA().setOnoff(true);
Tmp.getA().Notify();
wait();
}
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("XX+++"+j);
j++;
}
}




public synchronized void Notify() {
notify();
}

public static void main(String[] args) {

ThreadA A = new ThreadA();
Thread testA = new Thread(A);
testA.start();

ThreadTest B = new ThreadTest();
Thread testB = new Thread(B);
testB.start();

Tmp tmp = new Tmp();
tmp.setB(B);
tmp.setA(A);


}


}


class Tmp {
private static ThreadTest B;
private static ThreadA A;

public static ThreadA getA() {
return A;
}


public static void setA(ThreadA a) {
A = a;
}


public static ThreadTest getB() {
return B;
}


public static void setB(ThreadTest b) {
B = b;
}
}


class ThreadA implements Runnable {


boolean Onoff = false;


public boolean setOnoff(boolean LnKai) {
return Onoff = LnKai;
}


public synchronized void Notify() {
notify();
}



public void run() {
int j = 0;
while (true) {

while (Onoff) {

try {
synchronized (this) {
if (j == 5) {
j=0;
Onoff = false;
Tmp.getB().Notify();
wait();
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("YY---"+j);
j++;

}
}


}
}

結果:

XX+++0
XX+++1
XX+++2
XX+++3
XX+++4
YY---0
YY---1
YY---2
YY---3
YY---4
XX+++0
XX+++1
XX+++2
XX+++3
XX+++4
YY---0
YY---1
YY---2
YY---3
YY---4
XX+++0
XX+++1
XX+++2
XX+++3
XX+++4
YY---0
YY---1
YY---2
YY---3
YY---4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章