多个线程顺序进行和轮询进行的问题

废话不说了,晚上看到一个问题,需要ABC线程,能依次打印ABC(需要循环的)?

之前看能没有看到它需要循环打出这些,只是觉得他要顺序打出,所以就用了join 这个关键字做了一个demo;

package com.zeng.thread;
/**
 * 实现三个线程ABC,按照顺序输出ABC
 * 方案一
 * 
 * @author leo-zeng
 *
 */
public class Test1 {
	public static void main(String[] args) throws Exception {
		//A 线程
		Thread t1 = new Thread(new Runnable() {
			
			public void run() {
				System.out.println("A");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"A");
		
		//b线程
		Thread t2 = new Thread(new Runnable() {

			public void run() {
				System.out.println("B");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "B");
		
		//C 线程
		Thread t3 = new Thread(new Runnable() {
			
			public void run() {
				System.out.println("C");
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		},"C");
		
		t1.start();
		t1.join();
		t2.start();
		t2.join();
		t3.start();
	}
}
打印出的结果是ABC

后来仔细一看,不满足,问题的条件是需要循环打出ABC,所以用join 是不满足的,这会只能用到lock,通过condition来满足这个需求;下面也写了这个demo,

demo比较简单,通过一个reentrantLock 和 condition 的await 和single 方法,进行线程间的轮询,来达到结果;看下demo吧!

package com.zeng.thread;

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

/**
 * Abc 三个线程分别输出循环输出 abc 用reentrantLock
 * 
 * @author leo-zeng
 * 
 */
public class ReentrantLockDemo {
	public static void main(String[] args) {
		// 重入锁
		final ReentrantLock rLock = new ReentrantLock();
		// 三个condition
		final Condition aCon = rLock.newCondition();
		final Condition bCon = rLock.newCondition();
		final Condition cCon = rLock.newCondition();
		//线程a
		Thread a = new Thread(new Runnable() {

			public void run() {
				try {
					//先给第一个线程加上一段时间,给bc 线程 加锁 ,加信号
					Thread.sleep(500);
					} catch (InterruptedException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
					}
				for (int i = 0; i < 3; i++) {
					try {
						// 加上锁
						rLock.lock();
						System.out.println("A");
						Thread.sleep(1000);
						// 打印完A 之后 关闭a 给b 信号
						bCon.signal();
						//等待一个a给的信号
						try {
							aCon.await();
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						// 解锁
						rLock.unlock();
					}
				}
			}
		}, "A");
		//线程b
		Thread b = new Thread(new Runnable() {

			public void run() {
				for (int i = 0; i < 3; i++) {
					try {
						// 加上锁
						rLock.lock();
						try {
							//等待bcon
							bCon.await();
						} catch (InterruptedException e2) {
							e2.printStackTrace();
						}
						System.out.println("B");
						Thread.sleep(1000);
						// 打印完b 之后  给c 信号
						cCon.signal();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						// 解锁
						rLock.unlock();
					}
				}
			}
		}, "B");
		//线程c
		Thread c = new Thread(new Runnable() {

			public void run() {
				for (int i = 0; i <3; i++) {
					try {
						// 加上锁
						rLock.lock();
						try {
							//等待ccon
							cCon.await();
						} catch (InterruptedException e2) {
							e2.printStackTrace();
						}
						System.out.println("C");
						Thread.sleep(1000);
						// 打印完c 之后  给a 信号
						aCon.signal();
					} catch (InterruptedException e) {
						e.printStackTrace();
					} finally {
						// 解锁
						rLock.unlock();
					}
				}
			}
		}, "C");
		//a b c 线程
		a.start();
		b.start();
		c.start();
		Thread.yield();
	}
}
这里就不重点介绍ReentrantLock锁了;下次再和synchronized 一起对比介绍!


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值