实现A1B2交替打印(交替执行),彻底理解synchronized,wait,notify,volatile,ReentrantLock,Condition,Semaphore,SupportLock

java多线程实现A到Z和1到26按顺序输出,比如1A2B3C4D......

今天研究了一下,控制两个线程交替执行输出,一个输出1-26,一个输出A到Z.

用了6种方法实现:

1:使用synchronized,wait,notify

package ef.thread;

/**
 * @author :sunshine
 * @date :Created in 2021/7/20 16:13
 * @description:AZ26
 */
public class AZ26 {
	public static void main(String[] args) {
		final Object lock = new Object();
		Thread t1 = new Thread(() -> {
			int i = 1;
			synchronized (lock) {
				while (i <= 26) {
					System.out.println(i);
					i++;
					try {
						lock.notify();
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				lock.notify();//必须,否则无法停止程序,因为最后肯定有个在wait
			}
		});
		Thread t2 = new Thread(() -> {
			char i = 'A';
			synchronized (lock) {
				while (i <= 'Z') {
					System.out.println(i);
					i++;
					try {
						lock.notify();
						lock.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				lock.notify();//必须,否则无法停止程序,因为最后肯定有个在wait
			}
		});
		t1.start();
		t2.start();
	}
}

2:使用volatile,保证线程可见性

package ef.thread;

/**
 * @author :sunshine
 * @date :Created in 2021/7/20 16:13
 * @description:AZ26
 */
public class AZ262 {
	static volatile boolean thread2Run = false;
	public static void main(String[] args) {

		Thread t1 = new Thread(() -> {
			int i = 1;
			while(i <= 26){
				System.out.println(i);
				i++;
				thread2Run = true;
				while(true){
					if(!thread2Run) break;
				}
			}
		});
		Thread t2 = new Thread(() -> {
			char i = 'A';
			while(i <= 'Z'){
				while(thread2Run){
					System.out.println(i);
					i++;
					thread2Run = false;
					break;
				}
			}
		});
		t1.start();
		t2.start();
	}
}

3:使用ReentrantLock的Condition

package ef.thread;

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

/**
 * @author :sunshine
 * @date :Created in 2021/7/21 10:18
 * @description:AZ263
 */
public class AZ263 {

	public static void main(String[] args) {
		ReentrantLock lock = new ReentrantLock();
		Condition c1 = lock.newCondition();
		Condition c2 = lock.newCondition();
		Thread t1 = new Thread(() -> {
			int i = 1;
			lock.lock();
			while (i <= 26) {
				System.out.println(i);
				i++;
				try {
					c2.signal();    //唤醒线程2
					c1.await();     //等待且释放线程1当前的锁
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			c2.signal();    //唤醒线程2,必须,否则无法停止程序,因为最后肯定有个在wait
			lock.unlock();
		});
		Thread t2 = new Thread(() -> {
			char i = 'A';
			lock.lock();
			while (i <= 'Z') {
				System.out.println(i);
				i++;
				try {
					c1.signal();    //唤醒线程1
					c2.await();     //等待且释放线程2当前的锁
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			c1.signal();    //唤醒线程2,必须,否则无法停止程序,因为最后肯定有个在wait
			lock.unlock();
		});
		t1.start();
		t2.start();
	}
}

4:使用LockSupport

package ef.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * @author :sunshine
 * @date :Created in 2021/7/21 10:18
 * @description:AZ263
 */
public class AZ264 {
	static Thread t1 = null, t2 = null;
	public static void main(String[] args) {

		t1 = new Thread(() -> {
			int i = 1;
			while(i <= 26){
				System.out.println(i);
				LockSupport.unpark(t2); //t2可以跑了
				LockSupport.park();		//t1停住等待

				i++;
			}
		});
		t2 = new Thread(() -> {
			char i = 'A';
			while(i <= 'Z'){
				LockSupport.park();	//t2等待
				System.out.println(i);
				LockSupport.unpark(t1); //t1可以跑了,t2挺住等待

				i++;
			}
		});
		t1.start();
		t2.start();
	}
}

5:使用semaphore

package ef.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * @author :sunshine
 * @date :Created in 2021/7/26 17:24
 * @description:AZ265
 */
public class AZ265 {

	public static void main(String[] args) {
		final CountDownLatch latch = new CountDownLatch(2);
		Semaphore semaphore = new Semaphore(1, true);
		// semaphore只是控制信号量,每次只能被固定数量的线程拿到,不公平的话,被谁拿到不一定
		// 所以这里用锁控制两个线程都启动后再开始执行公平的争抢信号量
		Thread t1 = new Thread(() -> {
			latch.countDown(); // 门闩减1
			try {
				latch.await(); // 等待门闩=0开始执行
				int i = 1;
				while (i <= 26) {
					semaphore.acquire();
					System.out.println(i);
					semaphore.release();
					i++;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

		Thread t2 = new Thread(() -> {
			latch.countDown(); // 门闩减1
			try {
				latch.await(); // 等待门闩=0开始执行
				char i = 'A';
				while (i <= 'Z') {
					semaphore.acquire();
					System.out.println(i);
					semaphore.release();
					i++;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

		t1.start();
		t2.start();

	}
}

6:使用SynchronousQueue(容量永远为0,且阻塞)

package ef.thread;

import java.util.concurrent.SynchronousQueue;

/**
 * @author :sunshine
 * @date :Created in 2021/7/21 10:18
 * @description:AZ266
 */
public class AZ266 {
	public static void main(String[] args) {
		SynchronousQueue<Integer> sq1 = new SynchronousQueue<>();
		SynchronousQueue<Character> sq2 = new SynchronousQueue<>();
		 Thread t1 = new Thread(() -> {
			try {
				int i = 1;
				while (i <= 26) {
					sq1.put(i);
					System.out.println(sq2.take());
					i++;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
		Thread t2 = new Thread(() -> {
			try {
				char i = 'A';
				while (i <= 'Z') {
					System.out.println(sq1.take());// 保证先打印1
					sq2.put(i);
					i++;
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});
		t1.start();
		t2.start();
	}
}

目前只想到这几种方法,都可以实现这个功能,有其他更好的方案,欢迎讨论。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

学编程的司马光

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

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

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

打赏作者

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

抵扣说明:

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

余额充值