三个线程顺序循环执行的3种方式

第一种方式:使用Object的wait和notifyAll方法

package printABC.method1;

//第一种方法,使用Object的wait和notifyAll方法
public class TestPrint {
	static int count = 0;
	static final Object obj = new Object();
	Thread t1 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 0) {
						System.out.println("A");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});
	Thread t2 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 1) {
						System.out.println("B");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});
	Thread t3 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 2) {
						System.out.println("C");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});

	public void fun() {
		t3.start();
		t1.start();
		t2.start();
	}

	public static void main(String[] args) {
		TestPrint tp = new TestPrint();
		long t1 = System.currentTimeMillis();
		tp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)// 运行10个毫秒
				System.exit(0);
		}
	}
}

第二种方法:使用lock和condition

package printABC.method2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//使用lock和Condition,更加灵活
public class LockPrint {
	private static int count = 0;
	private Lock lock = new ReentrantLock();
	Condition c1 = lock.newCondition();
	Condition c2 = lock.newCondition();
	Condition c3 = lock.newCondition();
	Thread t1 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 0)
						c1.await();
					System.out.println("A");
					count++;
					c2.signal();// 唤醒条件2
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});
	Thread t2 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 1)
						c2.await();
					System.out.println("B");
					count++;
					c3.signal();// 唤醒条件3
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});
	Thread t3 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 2)
						c3.await();
					System.out.println("C");
					count++;
					c1.signal();// 唤醒条件1
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});

	public void fun() {
		t3.start();
		t1.start();
		t2.start();
	}

	public static void main(String[] args) {
		LockPrint lp = new LockPrint();
		long t1 = System.currentTimeMillis();
		lp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)
				System.exit(0);
		}
	}
}

第三种方法:使用concurrent的信号量Semaphore

package printABC.method3;

import java.util.concurrent.Semaphore;

//使用信号量
public class ConcurrentPrint {
	// 共享资源个数都初始为1
	private static Semaphore s1 = new Semaphore(1);
	private static Semaphore s2 = new Semaphore(1);
	private static Semaphore s3 = new Semaphore(1);
	Thread t1 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s1.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("A");
				s2.release();
			}
		}
	});
	Thread t2 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s2.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("B");
				s3.release();
			}
		}
	});
	Thread t3 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s3.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("C");
				s1.release();
			}
		}
	});

	public void fun() throws InterruptedException {
		// 先占有输出BC的线程的信号量计数
		// 则只能从输出A的线程开始。获取信号量A,然后释放B-获取B-释放C-获取C-释放A,由此形成循环
		s2.acquire();
		s3.acquire();
		t2.start();
		t3.start();
		t1.start();
	}

	public static void main(String[] args) throws InterruptedException {
		ConcurrentPrint cp = new ConcurrentPrint();
		long t1 = System.currentTimeMillis();
		cp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)
				System.exit(0);
		}
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值