当前位置:网站首页>阿里一个面试题:使用两个线程,交替输出字母和数字
阿里一个面试题:使用两个线程,交替输出字母和数字
2022-06-27 12:10:00 【wgslucky】

- 使用LockSupport
LockSupport类,是JUC包中的一个工具类,是用来创建锁和其他同步类的基本线程阻塞原语。
LockSupport类的核心方法有两个:park()和unpark(),其中park()方法用来阻塞当前调用线程,unpark()方法用于唤醒指定线程。这其实和Object类的wait()和singal()方法有些类似,但是LockSupport的这两种方法从语义上将比Object类的方法更清晰,而且可以针对指定线程进行阻塞和唤醒。
/** * @author 王广帅 * @since 2022/6/26 11:01 */
public class LockSupportDemo {
static Thread t1 = null;
static Thread t2= null;
public static void main(String[] args) {
char[] charArray1 = "123456789".toCharArray();
char[] charArray2 = "ABCDEFGHI".toCharArray();
t1 = new Thread(()->{
for(char c : charArray1){
System.out.print(c);
// 叫醒线程t2
LockSupport.unpark(t2);
// 当前线程阻塞
LockSupport.park();
}
},"线程1");
t2 = new Thread(()->{
for(char c : charArray2){
// 阻塞当前线程
LockSupport.park();
System.out.print(c);
// 叫醒线程t1
LockSupport.unpark(t1);
}
},"线程2");
t1.start();
t2.start();
}
}
- 使用wait,notify
注意wait,notify必须在锁里面执行
/** * @author 王广帅 * @since 2022/6/26 11:12 */
public class WaitNotifyDemo {
public static void main(String[] args) {
final Object obj = new Object();
char[] charArray1 = "123456789".toCharArray();
char[] charArray2 = "ABCDEFGHI".toCharArray();
new Thread(()->{
synchronized (obj) {
for (char c : charArray1) {
System.out.print(c);
try {
obj.notify();
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
}
},"线程1").start();
new Thread(()->{
synchronized (obj) {
for (char c : charArray2) {
System.out.print(c);
try {
obj.notify();
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
obj.notify();
}
},"线程2").start();
}
}
- 使用Lock Condition
/** * @author 王广帅 * @since 2022/6/26 13:26 */
public class LockConditionDemo {
public static void main(String[] args) {
char[] charArray1 = "123456789".toCharArray();
char[] charArray2 = "ABCDEFGHI".toCharArray();
Lock lock = new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2= lock.newCondition();
new Thread(()->{
lock.lock();
try{
for(char c: charArray1){
System.out.print(c);
condition2.signal();
condition1.await();
}
condition2.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
},"线程1").start();
new Thread(()->{
lock.lock();
try{
for(char c: charArray2){
System.out.print(c);
condition1.signal();
condition2.await();
}
condition1.signal();
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
},"线程2").start();
}
}
- 使用TransferQueue
TransferQueue(java7引入)继承了BlockingQueue(BlockingQueue又继承了Queue)并扩展了一些新方法。生产者会一直阻塞直到所添加到队列的元素被某一个消费者所消费(不仅仅是添加到队列里就完事)。
/** * @author 王广帅 * @since 2022/6/26 13:49 */
public class TransferQueueDemo {
public static void main(String[] args) {
char[] charArray1 = "123456789".toCharArray();
char[] charArray2 = "ABCDEFGHI".toCharArray();
TransferQueue<Character> queue = new LinkedTransferQueue<>();
new Thread(()->{
try{
for(char c : charArray1){
System.out.print(queue.take());
queue.transfer(c);
}
}catch (Exception e){
e.printStackTrace();
}
},"线程1").start();
new Thread(()->{
try{
for(char c: charArray2){
queue.transfer(c);
System.out.print(queue.take());
}
}catch (Exception e){
e.printStackTrace();
}
},"线程2").start();
}
}
以上示例来自马士兵老师的课程
边栏推荐
- Comment modifier Node Fichiers dans les modules
- How to modify a node_ Files in modules
- Interview shock 60: what will cause MySQL index invalidation?
- Picocli getting started
- 浏览器输入url地址,到页面渲染发生了什么
- 57. The core principle of flutter - layout process
- 关于枚举类的两种用法
- 如何修改 node_modules 里的文件
- nifi从入门到实战(保姆级教程)——身份认证
- Talk about go language and cloud native technology
猜你喜欢
随机推荐
号称史上最难618,淘宝数据盘点你做对了吗?
nifi从入门到实战(保姆级教程)——身份认证
Quanzhi A13 tossing memo
.NET6接入Skywalking链路追踪完整流程
最短编辑距离(线性dp写法)
Dynamic programming [III] (interval DP) stone merging
Deep understanding of happens before principle
How to participate in openharmony code contribution
. Net6 access skywalking link tracking complete process
Introduce you to ldbc SNB, a powerful tool for database performance and scenario testing
居家办公被催之后才明白的时间管理
Configuration of thymeleaf
What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?
C # WPF realizes undo redo function
DM8:达梦数据库-锁超时
MySQL learning 1: installing MySQL
私藏干货分享:关于企业架构中如何进行平台化
ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络
浏览器输入url地址,到页面渲染发生了什么
【粉丝福利】今天给大家介绍一个白捡钱的方法-可转债,本人亲自验证,每年每人能获利1500元









