关于交替打印

2020-03-07  本文已影响0人  唐执

上代码,利用volatile的特性;

package com.tangzhi.example;

public class PrintTest {

public static volatile int OPEN =1;

    public static final void main(String[] agrs)throws InterruptedException {

Thread thread1 =new Thread(new Runnable() {

            public void run() {

                int cnt = 10;

                while(cnt-- > 0) {

                    if (OPEN == 1) {

                        System.out.println("==>1");

                        OPEN = 2;

                    } else {

                        while (OPEN != 1) {

                            try {

                                Thread.sleep(100);

                            } catch (InterruptedException e) {

                                e.printStackTrace();

                            }

}

}

}

}

        });

        Thread thread2 =new Thread(new Runnable() {

            public void run() {

                int cnt = 10;

                while(cnt-- > 0) {

                    if (OPEN == 2) {

                        System.out.println("==>2");

                        OPEN = 1;

                    } else {

                        try {

                            Thread.sleep(100);

                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }

}

}

}

        });

        thread1.start();

        thread2.start();

        thread1.join();

        thread2.join();

    }

}

结果:GOOD

==>1

==>2

==>1

==>2

==>1

==>2

==>1

==>2

==>1

==>2


用锁的,再来一个;注意wait的面试题

package com.tangzhi.example;

public class PrintSyncTest {

public static volatile IntegerOPEN =1;

    public static final void main(String[] agrs)throws InterruptedException {

Thread thread1 =new Thread(new Runnable() {

            public void run() {

                int cnt = 10;

                while(cnt-- > 0) {

                    synchronized (OPEN){

                        try {

                            OPEN.wait(1000);

                            System.out.println("==>1");

                            OPEN = 2;

                        }catch (Exception e) {

                            System.out.println("1 ==>" + e);

                        }

}

}

}

        });

        Thread thread2 =new Thread(new Runnable() {

            public void run() {

                int cnt = 10;

                while(cnt-- > 0) {

                    synchronized (OPEN){

                        try {

                            OPEN.wait(1000);

                            System.out.println("==>2");

                            OPEN = 1;

                        }catch (Exception e) {

                            System.out.println("2 ==>" + e);

                        }

}

}

}

        });

        thread1.start();

        thread2.start();

        thread1.join();

        thread2.join();

    }

}

结果:好样的

==>2

==>1

==>2

==>2

==>1

==>2

==>1

==>2

==>1

==>2

==>1

上一篇 下一篇

猜你喜欢

热点阅读