码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-01-14 19:09:20      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:package   ++   mon   current   boolean   偶数   ram   start   tar   

一、采用对象的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();

    }

}

 

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

标签:package   ++   mon   current   boolean   偶数   ram   start   tar   

原文地址:https://www.cnblogs.com/gaopengpy/p/12193575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!