如何在多线程环境下 让 一个类中的的 3个方法 顺序执行,并且循环下去

方式一:  synchronized

public class ThreadCondition {

private int state;

public synchronized void a(){

while (state != 0) {

try {

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("a");

state = 1;

notifyAll();

}

public synchronized void b(){

while (state != 1) {

try {

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("b");

state = 2;

notifyAll();

}

public synchronized void c(){

while (state != 2) {

try {

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("c");

state = 0;

notifyAll();

}

}

class A implements Runnable{

private ThreadCondition condition;

public A(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.a();

}

}

class B implements Runnable{

private ThreadCondition condition;

public B(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.b();

}

}

class C implements Runnable{

private ThreadCondition condition;

public C(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.c();

}

}


方式二:

public class ThreadCondition {

private int state;

private Lock lock = new ReentrantLock();

Condition a = lock.newCondition();

Condition b = lock.newCondition();

Condition c = lock.newCondition();

public  void a(){

lock.lock();

while (state != 0) {

try {

a.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("a");

state = 1;

b.signal();

lock.unlock();

}

public  void b(){

lock.lock();

while (state != 1) {

try {

b.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("b");

state = 2;

c.signal();

lock.unlock();

}

public  void c(){

lock.lock();

while (state != 2) {

try {

c.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("c");

state = 0;

a.signal();

lock.unlock();

}

}

class A implements Runnable{

private ThreadCondition condition;

public A(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.a();

}

}

class B implements Runnable{

private ThreadCondition condition;

public B(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.b();

}

}

class C implements Runnable{

private ThreadCondition condition;

public C(ThreadCondition condition){

this.condition = condition;

}

public void run() {

while(true)

condition.c();

}

}



wait/notify 和 condition的区别是 :

notifyAll 唤醒的线程是随机的, 而 condition可以指定 唤醒哪一个线程 

你可能感兴趣的:(如何在多线程环境下 让 一个类中的的 3个方法 顺序执行,并且循环下去)