面试:双线程交替打印奇偶数

代码如下:

package practice1;

/**
 * 0-100的奇数偶数打印
 * 1、通过对象的wait和notify进行线程阻塞
 * 2、通过对num%2的结果进行奇数偶数的判断输出
 *
 */
public class JiOuOne {

    private static volatile int num = 0;

    private static final int max = 100;

    public static void main(String[] args) {
        String monitor = "monitor";
        // 偶数线程
        Thread oddThread = new Thread(new OddNumber(monitor));
        oddThread.setName("偶数打印:");

        // 奇数线程
        Thread evenThread = new Thread(new EvenNumber(monitor));
        evenThread.setName("奇数打印:");

        oddThread.start();
        evenThread.start();
    }


    /**
     * 打印偶数
     */
    static class OddNumber implements Runnable {

        private String monitor;

        public OddNumber(String monitor) {
            this.monitor = monitor;
        }

        @Override
        public void run() {
            while (num <= max) {
                synchronized (monitor) {
                    // 如果num%2有余数,则除不尽,则阻塞
                    while (num % 2 != 0) {
                        try {
                            monitor.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "->" + num);
                    num++;
                    // 唤醒监视器
                    monitor.notify();
                }
            }
        }
    }


    /**
     * 打印奇数
     */
    static class EvenNumber implements Runnable {

        private String monitor;

        public EvenNumber(String monitor) {
            this.monitor = monitor;
        }

        @Override
        public void run() {
            while (num < max) {
                synchronized (monitor) {
                    // 如果num%2余数为0,则可以除尽,阻塞
                    while (num % 2 == 0) {
                        try {
                            monitor.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName() + "->" + num);
                    num++;
                    // 唤醒对象监视器
                    monitor.notify();
                }
            }
        }
    }

}

运行结果:

面试:双线程交替打印奇偶数_第1张图片

你可能感兴趣的:(面试,java,jvm)