多线程实现轮流打印的两种方式(thread和ReentrantLock)

thread

/**
 * 双线程轮流打印
 * @author Administrator
 *线程1负责打印a,b,c,d
 *线程2负责打印1,2,3,4
 */
public class thread {

	static final Object object = new Object();
	public static void main(String[] args) {
		
		new Thread(new Runnable() {
			String[] a = {"a","b","c","d"};
			public void run() {
				// TODO Auto-generated method stub
				for(int i=0;i<4;i++)
				{
					synchronized(object){
						System.out.println("线程A开始执行");
						object.notify();
						try {
							System.out.println("线程a开始等待");
							object.wait();
						} catch (Exception e) {
							e.printStackTrace();
							// TODO: handle exception
						}
						System.out.println("线程a继续执行");
						System.out.println(a[i]);
						System.out.println("线程A执行结束!");
						object.notify();
					}
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			int[] a = {1,2,3,4};
			public void run() {
				// TODO Auto-generated method stub
				for(int i =0;i<a.length;i++){
					synchronized(object){
						System.out.println("线程1开始执行!");
						object.notify();
						try {
							System.out.println("线程1开始等待");
							object.wait();
						} catch (Exception e) {
							e.printStackTrace();
							// TODO: handle exception
						}
						System.out.println("线程1继续执行!");
						System.out.println(a[i]);
						System.out.println("线程1执行结束!");
					}
					
				}
			}
		}).start();
	}
}

 ReentrantLock


/**
 *  1.开启两个线程,一个线程打印A~Z,两一个线程打印1~52的数据。

 2.实现交替打印,输出结果为12A34B...........5152Z.

 3.请用多线程方式实现。
 * @author Administrator
 *
 */
public class thread2 {

	static class DataPrint{
		public boolean  letterFlag = true;
		public boolean numFlag = true;
		
		int num = 1;
		int letter = 65;
		
		boolean waitFlag = true;
		
		Lock lock = new ReentrantLock();
		Condition conLetrter = lock.newCondition();
		Condition conNum = lock.newCondition();
		
		public void printLetter(){
			if(letter>90){
				letterFlag = false;
				return;
			}
			lock.lock();
			try {
				if(waitFlag){
					conLetrter.await();
				}
				System.out.println((char)letter);
				letter++;
				Thread.sleep(1000);
				waitFlag = true;
				conNum.signal();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			finally{
				lock.unlock();
			}
		}
		
		public void printerNum(){
			if(num >52){
				numFlag = false;
				return;
			}
			lock.lock();
			try {
				if(!waitFlag)
					conNum.await();
				System.out.println(num);
				num++;
				System.out.println(num);
				num++;
				Thread.sleep(1000);
				waitFlag  =false;
				conLetrter.signal();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			finally{
				lock.unlock();
			}
		}
	}
	
	public static void main(String[] args) {
		final DataPrint dataPrint = new DataPrint();
		
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				while(dataPrint.letterFlag)
					dataPrint.printLetter();
			}
		}).start();
		
		new Thread(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				while(dataPrint.numFlag)
					dataPrint.printerNum();
			}
		}).start();
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月星辰_w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值