【面试题】两个线程交替打印1A2B3C....26Z(多种方法实现)

1.LockSupport

package com.example.demo;

import java.util.concurrent.locks.LockSupport;

/**
 * 交替打印1A2B3C......26Z
 */
public class Thread_LockSupport extends Thread {
    static Thread t1 = null, t2 = null;
    @Override
    public void run() {

    }

    public static void main(String[] args) throws Exception{
        t1 = new Thread(()->{
            for(int i = 1;i<=26;i++){
                System.out.print(i);
                LockSupport.unpark(t2);
                LockSupport.park();
            }
        }, "t1");
        t2 = new Thread(()->{
            for(char p = 'A'; p <= 'Z'; p++) {
                LockSupport.park();
                System.out.print(p);
                LockSupport.unpark(t1);
            }
        },"'t2");
        t1.start();
        t2.start();


    }
}

2.BlockingQueue

package com.example.demo;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class Thread_TransferQueue extends Thread {
    private static BlockingDeque<String> bq1 = new LinkedBlockingDeque<String>(1);
    private static BlockingDeque<String> bq2 = new LinkedBlockingDeque<String>(1);
    static Thread t1 = null, t2 = null;

    @Override
    public void run() {

    }

    public static void main(String[] args){
        t1 = new Thread(()->{
            for(int i = 1;i<=26;i++){
                try {
                    bq1.put(i+"");
                    System.out.print(bq2.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "t1");
        t2 = new Thread(()->{
            for(char p = 'A'; p <= 'Z'; p++) {
                try {
                    System.out.print(bq1.take());
                    bq2.put(p+"");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"'t2");
        t1.start();
        t2.start();


    }
}

3.synchronized wait notify

package com.example.demo;

public class Thread_sync extends Thread {
    static Thread t1 = null, t2 = null;
    private volatile static Object obj = new Object();
    @Override
    public void run() {

    }

    public static void main(String[] args){
        t1 = new Thread(()->{
            for(int i = 1;i<=7;i++){
                synchronized (obj){
                    System.out.print(i);
                    try {
                        obj.notify();
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    obj.notify();//必须要有,不然总会有一个线程最后处于阻塞状态
                }
            }
        }, "t1");
        t2 = new Thread(()->{
            for(char p = 'A'; p <= 'G'; p++) {
                synchronized (obj){
                    System.out.print(p+"");
                    try {
                        obj.notify();
                        obj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    obj.notify();//必须要有,不然总会有一个线程最后处于阻塞状态
                }
            }
        },"'t2");
        t1.start();
        t2.start();


    }
}

4.Lock Condition

package com.example.demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
 * 可保证先打印数字
 */
public class Thread_LockCondition2 extends Thread {
    static Thread t1 = null, t2 = null;
    static ReentrantLock lock = new ReentrantLock(true);
    static Condition condition1 = lock.newCondition();
    static Condition condition2 = lock.newCondition();
    private static volatile Boolean ifT2Start = false;
    @Override
    public void run() {

    }

    public static void main(String[] args){
        t1 = new Thread(()->{
            try {
                lock.lock();
                while(!ifT2Start){
                    for(int i = 1;i<=7;i++){
                        System.out.print(i);
                        condition1.signal();
                        condition2.await();
                    }
                }
                condition1.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }, "t1");
        t2 = new Thread(()->{
                try {
                    lock.lock();
                    ifT2Start = true;
                    for(char p = 'A'; p <= 'G'; p++) {
                        System.out.print(p+"");
                        condition2.signal();
                        condition1.await();
                    }
                    condition2.signal();
                }catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
        },"'t2");

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


    }
}

5.TransferQueue

package com.example.demo;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;

public class Thread_TransferQueue extends Thread {
    private static TransferQueue<String> tq = new LinkedTransferQueue<String>();
    static Thread t1 = null, t2 = null;

    @Override
    public void run() {

    }

    public static void main(String[] args){
        t1 = new Thread(()->{
            for(int i = 1;i<=7;i++){
                try {
                    tq.transfer(i+"");
                    System.out.print(tq.take());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }, "t1");
        t2 = new Thread(()->{
            for(char p = 'A'; p <= 'G'; p++) {
                try {
                    System.out.print(tq.take());
                    tq.transfer(p+"");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"'t2");
        t1.start();
        t2.start();


    }
}

6.自旋

package com.example.demo;

public class Thread_self extends Thread {
    static volatile int x = 0;
    static Thread t1 = null, t2 = null;

    @Override
    public void run() {

    }

    public static void main(String[] args){
        t1 = new Thread(()->{
            for(int i = 1;i<=7;i++){
                while(x!=0){}
                System.out.print(i+"");
                x = 1;
            }
        }, "t1");
        t2 = new Thread(()->{
            for(char p = 'A'; p <= 'G'; p++) {
                while(x!=1){}
                System.out.print(p+"");
                x = 0;
            }
        },"'t2");
        t1.start();
        t2.start();


    }
}

 

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值