多线程面试题——两个线程交替打印

多线程面试题——两个线程交替打印

synchronized 两个线程交替打印

//一个线程打印1 2 3 ...
//一个线程打印a b c ...
//交替打印 1 a 2 b 3 c  ... 直到所有字母打印完毕
public class Thread1 {
    private static int index = 1;
    private  static Object o = new Object();

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                synchronized (o){
                    while(index<=26){
                        System.out.println(index++);
                        //唤醒B线程
                        o.notifyAll();

                        if(index==27){
                            //最后一次不能再等待,否则会死等
                            break;
                        }

                        //阻塞自己
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                synchronized (o) {
                    while(index<=27){
                        char c = (char) ('a' + (index - 2));
                        System.out.println(c);

                        if(index==27){
                            //最后一次不能再等待,否则会一直死等
                            break;
                        }
                        //唤醒A线程
                        o.notifyAll();

                        //阻塞自己
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }.start();
    }
    

}

volatile两个线程交替执行

//一个线程打印1 2 3 ...
//一个线程打印a b c ...
//交替打印 1 a 2 b 3 c  ... 直到所有字母打印完毕
public class Thread3 {
    private static int index = 1;
    private static volatile boolean f = true;

    public static void main(String[] args) {
        new Thread(){
            @Override
            public void run() {
                while(index<=26){
                    if(f){
                        System.out.println(index++);
                        f = false;
                    }
                }
            }
        }.start();

        new Thread(){
            @Override
            public void run() {
                while(index<=27){
                    if(!f){
                        System.out.println((char)('a'+index-2));
                        f = true;
                        if(index==27)break;
                    }

                }
            }
        }.start();
    }
}

volatile三个线程交替执行

代码鬼才,手动滑稽,利用volatile好像可以实现无论多少线程都可以交替执行,好不要脸的解法额哦

//一个线程打印1 2 3 ...
//一个线程打印a b c ...
//一个线程打印A B C ...
//交替打印 1 a A 2 b B 3 c C ... 直到所有字母打印完毕
public class Thread4 {
    private static int index = 1;
    private static volatile int f = 0;
    
    public static void main(String[] args){

        new Thread(){
            @Override
            public void run() {
                while(index<=26){
                    if(f==0){
                        System.out.println(index++);
                        f = 1;
                    }
                }
            }
        }.start();


        new Thread(){
            @Override
            public void run() {
                while(index<=27){
                    if(f==1){
                        System.out.println((char)('a'+index-2));
                        f = 2;
                        if(index==27)break;
                    }

                }
            }
        }.start();


        new Thread(){
            @Override
            public void run() {
                while(index<=27){
                    if(f==2){
                        System.out.println((char)('A'+index-2));
                        f = 0;
                        if(index==27)break;
                    }
                }
            }
        }.start();

    }
}

LockSupport三个线程交替打印

//一个线程打印1 2 3 ...
//一个线程打印a b c ...
//一个线程打印A B C ...
//交替打印 1 a A 2 b B 3 c C ... 直到所有字母打印完毕
public class Thread2 {
    private static int index = 1;

    public static void main(String[] args) throws InterruptedException {
        A a = new A();
        Thread aThread = new Thread(a);
        B b = new B();
        Thread bThread = new Thread(b);
        C c = new C();
        Thread cThread = new Thread(c);

        a.unparkThread = bThread;
        b.unparkThread = cThread;
        c.unparkThread = aThread;

        aThread.start();
        Thread.sleep(1);
        bThread.start();
        Thread.sleep(1);
        cThread.start();
    }

    static class A implements Runnable{
        public Thread unparkThread;
        @Override
        public void run() {
                while(index<=26){
                    System.out.println(index++);
                    //唤醒B线程
                    LockSupport.unpark(unparkThread);

                    if(index==27){
                        //最后一次不能再等待,否则会死等
                        break;
                    }

                    //阻塞自己
                    LockSupport.park();
                }
        }
    }

    static class B implements Runnable{
        public Thread unparkThread;
        @Override
        public void run() {
                while(index<=27){
                    char c = (char) ('a' + (index - 2));
                    System.out.println(c);

                    //唤醒C线程
                    LockSupport.unpark(unparkThread);

                    if(index==27){
                        //最后一次不能再等待,否则会一直死等
                        break;
                    }


                    //阻塞自己
                    LockSupport.park();

                }
        }
    }

    static class C implements Runnable{
        public Thread unparkThread;
        @Override
        public void run() {
                while(index<=27){
                    char c = (char) ('A' + (index - 2));
                    System.out.println(c);

                    if(index==27){
                        //最后一次不能再等待,否则会一直死等
                        break;
                    }
                    //唤醒A线程
                    LockSupport.unpark(unparkThread);

                    //阻塞自己
                    LockSupport.park();

                }
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值