多个线程顺序执行的几种方法

参考:http://www.cnblogs.com/icejoywoo/archive/2012/10/15/2724674.html

1、信号量Sephmore

public class SyncThreadTest {

	private static Semaphore A = new Semaphore(1);
	private static Semaphore B = new Semaphore(1);
	private static Semaphore C = new Semaphore(1);
	public static void main(String[] args) {
		ThreadA a = new ThreadA();
		ThreadB b = new ThreadB();
		ThreadC c = new ThreadC();
		try {
			B.acquire();
			C.acquire();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		a.start();
		b.start();
		c.start();
		System.out.print("over\n");
	}

	static class ThreadA extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				for(int i = 0; i < 10; i++) {
					A.acquire();
					System.out.print("thread a run" + i + "\n");					
					B.release();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	static class ThreadB extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				for(int i = 0; i < 10; i++) {
					B.acquire();
					System.out.print("thread b run" + i + "\n");					
					C.release();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	static class ThreadC extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				for(int i = 0; i < 10; i++) {
					C.acquire();
					System.out.print("thread c run" + i + "\n");					
					A.release();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}


2、使用ReentrantLock

public class SyncThreadTestReentrantLock {
	private static Lock lock = new ReentrantLock();
	private static int state = 0;
	public static void main(String[] args) {
		ThreadA a = new ThreadA();
		ThreadB b = new ThreadB();
		ThreadC c = new ThreadC();
		a.start();
		b.start();
		c.start();
		/*try {
			a.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			b.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			c.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		System.out.print("over\n");
	}

	static class ThreadA extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			while(i < 10) {
				//System.out.print("thread a begin \n");
				lock.lock();
				//System.out.print("thread a lock \n");
				if (state % 3 == 0) {
					System.out.print("thread a run"  + "\n");
					state++;
					i++;
				}
				lock.unlock();
				//System.out.print("thread a end \n");
			}
		}
	}

	static class ThreadB extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			while(i < 10) {
				//System.out.print("thread b begin \n");
				lock.lock();
				//System.out.print("thread b lock \n");
				if (state % 3 == 1) {
					System.out.print("thread b run" + i + "\n");
					state++;
					i++;
				}
				lock.unlock();
				//System.out.print("thread b end \n");
			}
		}
	}

	static class ThreadC extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			while(i < 10) {
				//System.out.print("thread c begin \n");
				lock.lock();
				//System.out.print("thread c lock \n");
				if (state % 3 == 2) {
					System.out.print("thread c run" + i + "\n");
					state++;
					i++;
				}
				lock.unlock();
				//System.out.print("thread c end \n");
			}
		}
	}
}
3、ReetrantLock和Condition的组合
还可以使用condition, condition的效率可能会更高一些, await会释放lock锁, condition的await和signal与object的wait和notify方法作用类似

public class SyncThreadTestReentrantLock {
	private static Lock lock = new ReentrantLock();
	private static int state = 0;
	private static Condition A = lock.newCondition();
	private static Condition B = lock.newCondition();
	private static Condition C = lock.newCondition();
	public static void main(String[] args) {
		ThreadA a = new ThreadA();
		ThreadB b = new ThreadB();
		ThreadC c = new ThreadC();
		a.start();
		b.start();
		c.start();
		/*try {
			a.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			b.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			c.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		System.out.print("over\n");
	}

	static class ThreadA extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			lock.lock();
			try {
			while(i < 10) {
				//System.out.print("thread a begin \n");
				//System.out.print("thread a lock \n");
				if (state % 3 == 0) {
					System.out.print("thread a run"  + "\n");
					state++;
					i++;
					B.signal();
				} else {
						A.await();
				}
				//lock.unlock();
				//System.out.print("thread a end \n");
			}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
	}

	static class ThreadB extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			lock.lock();
			try {
			while(i < 10) {
				//System.out.print("thread a begin \n");
				//System.out.print("thread a lock \n");
				if (state % 3 == 1) {
					System.out.print("thread b run"  + "\n");
					state++;
					i++;
					C.signal();
				} else {
						B.await();
				}
				//lock.unlock();
				//System.out.print("thread a end \n");
			}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
	}

	static class ThreadC extends Thread {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			int i = 0;
			lock.lock();
			try {
			while(i < 10) {
				//System.out.print("thread a begin \n");
				//System.out.print("thread a lock \n");
				if (state % 3 == 2) {
					System.out.print("thread c run"  + "\n");
					state++;
					i++;
					A.signal();
				} else {
						C.await();
				}
				//lock.unlock();
				//System.out.print("thread a end \n");
			}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				lock.unlock();
			}
		}
	}
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值