Java n个线程轮流打印数字的问题

一、两个线程轮流打印数字。

加锁实现:

package lianxi;

/*
 * 用锁实现两个线程轮流打印1——100
 */
public class Print1TO100TwoThread {
	private Object lock = new Object();
	private int i = 0;

	Thread threadA = new Thread(new Runnable() {
		@Override
		public void run() {
			while (i <= 100) {
				synchronized (lock) {

					try {
						if (i > 100)
							break;
						System.out.println("threadA  :" + (i++));
						lock.notify();
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

	});

	Thread threadB = new Thread(new Runnable() {
		@Override
		public void run() {
			while (i <= 100) {
				synchronized (lock) {

					try {
						if (i > 100)
							break;
						System.out.println("threadB  :" + (i++));
						lock.notify();
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	});

	public void startTwoThread() throws InterruptedException {
		threadA.start();
		Thread.sleep(20);
		threadB.start();
	}
	public static void main(String[] args) throws InterruptedException {
		new Print1TO100TwoThread().startTwoThread();
	}

}

用锁效率太低,用一个变量来控制打印的顺序。

package lianxi;
/*
 * 用两个线程轮流打印1——10;用所实现效率太低,用变量来控制
 */
public class PrinntNumTwoThread {

	private volatile int num = 0;
	private volatile boolean flag = false;

	Thread threadA = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				if (num > 10)
					return;
				if (!flag) {
					System.out.println("threadA-->" + ":" + (num++));
					flag = !flag;
				}
			}
		}

	});

	Thread threadB = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (num > 10)
					return;
				if (flag) {
					System.out.println("threadB-->" + ":" + (num++));
					flag = !flag;
				}
			}
		}

	});

	public void startTwoThread() {
		threadA.start();
		threadB.start();
	}

	public static void main(String[] args) {
		new PrinntNumTwoThread().startTwoThread();
	}
}

二、那么如果要实现三个线程轮流打印1-100的数字呢?

package lianxi;

public class PrintNumThreeThread {
	private volatile int i = 0;
	private volatile int flag = 0;
	Thread threadA = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (i > 100)
					return;
				if (flag == 0) {
					System.out.println("threadA->" + ":" + (i++));
					flag = 1;
				}
			}
		}

	});

	Thread threadB = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (i > 100)
					return;
				if (flag == 1) {
					System.out.println("threadB->" + ":" + (i++));
					flag = 2;
				}
			}
		}

	});

	Thread threadC = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (i > 100)
					return;
				if (flag == 2) {
					System.out.println("threadC->" + ":" + (i++));
					flag = 0;
				}
			}
		}
	});

	public void startThreeThread() {
		threadA.start();
		threadB.start();
		threadC.start();
	}

	public static void main(String[] args) {
		new PrintNumThreeThread().startThreeThread();
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值