经典笔试题:两个线程交替打印奇偶数

一、采用对象的wait() notify()方法实现

package com.gaopeng.programming;

import java.util.concurrent.TimeUnit;

/**
 * 经典笔试题:交替打印奇偶数 采用对象的wait() notify()方法实现
 * 
 * @author gaopeng
 *
 */
public class OddEvenThread {
    private static volatile Integer counter = 0;
    public static Object monitor = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(new Runnable() {

            @Override
            public void run() {

                while (true) {

                    // 获取monitor共享资源的监视器锁
                    synchronized (monitor) {
                        System.out.println(counter++);
                        try {
                            monitor.notify();
                            monitor.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        Thread threadB = new Thread(new Runnable() {

            @Override
            public void run() {

                while (true) {
                    // 获取monitor共享资源的监视器锁
                    synchronized (monitor) {
                        System.out.println(counter++);

                        try {
                            monitor.notify();
                            monitor.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        threadA.start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threadB.start();

    }

}

二、采用volatile方法实现

package com.gaopeng.programming;

import java.util.concurrent.TimeUnit;

/**
 * 经典笔试题:交替打印奇偶数 采用volatile方法实现
 * 
 * @author gaopeng
 *
 */
public class OddEvenThreadVolatileVersion {

    private static volatile Integer counter = 0;
    private static volatile boolean loopForOdd = true;
    private static volatile boolean loopForEven = true;

    public static void main(String[] args) {
        Thread threadOdd = new Thread(new Runnable() {

            @Override
            public void run() {

                while (true) {

                    if (loopForOdd) {
                        System.out.println(counter++);
                        loopForEven = false;
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        Thread threadEven = new Thread(new Runnable() {

            @Override
            public void run() {

                while (true) {

                    if (loopForEven) {
                        System.out.println(counter++);
                        loopForOdd = false;
                    }

                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        });

        threadOdd.start();
        threadEven.start();

    }

}

猜你喜欢

转载自www.cnblogs.com/gaopengpy/p/12193575.html