Java三线程打印 ABC循环100次的2种实现方法

编写一个程序,开启 3 个线程 A,B,C,这三个线程的输出分别为 A、B、C,每个线程将自己的 输出在屏幕上打印 100 遍,要求输出的结果必须按顺序显示。如:ABCABCABC....

public class PrintABC {
	private static String message = "A";

	public static void main(String[] args) {

		Thread A = new Thread(() -> {
			int count = 1;
			while (count < 101) {
				while (!message.equals("A")) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println(count);
				System.out.println(message);
				message = "B";
				count = count + 1;
			}

		});

		Thread B = new Thread(() -> {
			int count = 1;
			while (count < 101) {
				while (!message.equals("B")) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println(count);
				System.out.println(message);
				message = "C";
				count = count + 1;
			}

		});
		Thread C = new Thread(() -> {
			int count = 1;
			while (count < 101) {
				while (!message.equals("C")) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				System.out.println(count);
				System.out.println(message);
				message = "A";
				count = count + 1;
			}

		});

		A.start();
		B.start();
		C.start();
	}
}

 

 

public class PrintABC2notifyAll {
	private volatile static String message = "A";
	static Object lock = new Object();

	public static void main(String[] args) {

		Thread A = new Thread(() -> {
			int count = 1;
			synchronized (lock) {
				while (count < 101) {

					while (!message.equals("A")) {
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					System.out.println(count + message);
					message = "B";
					count = count + 1;
					lock.notifyAll();
				}
			}

		});

		Thread B = new Thread(() -> {
			int count = 1;
			synchronized (lock) {
				while (count < 101) {
					while (!message.equals("B")) {
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					System.out.println(count + message);
					message = "C";
					count = count + 1;
					lock.notifyAll();
				}
			}

		});
		Thread C = new Thread(() -> {
			int count = 1;
			synchronized (lock) {
				while (count < 101) {
					while (!message.equals("C")) {
						try {
							lock.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
					System.out.println(count + message);
					message = "A";
					count = count + 1;
					lock.notifyAll();
				}
			}

		});

		A.start();
		B.start();
		C.start();
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值