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

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

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();

                }
        }
    }

}

发布了127 篇原创文章 · 获赞 68 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/LiuRenyou/article/details/103861499