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

java两线程交替打印奇偶数

时间:2020-08-24 15:13:40      阅读:42      评论:0      收藏:0      [点我收藏+]

标签:-o   span   interrupt   实验   tar   while   read   thread   static   

 

方法1:synchronized

class Odd implements Runnable {
        @Override
        public void run() {
            while(idx < len){
                synchronized (lock){
                    if (idx < len && (num[idx] >> 1 << 1) != num[idx]){
                        System.out.print(num[idx]);
                        System.out.println("--Odd");
                        ++idx;
                    }
                }
            }
        }
    }

class Even implements Runnable {
        @Override
        public void run() {
            while(idx < len){
                synchronized (lock){
                    if (idx < len && (num[idx] >> 1 << 1) == num[idx]){
                        System.out.print(num[idx]);
                        System.out.println("--Even");
                        ++idx;
                    }
                }
            }
        }
    }

 

方法2:ReentrantLock

    class Odd implements Runnable {
        @Override
        public void run() {
            while(idx < len){
                rlock.lock();
                if (idx < len && (num[idx] >> 1 << 1) != num[idx]){
                    System.out.print(num[idx]);
                    System.out.println("--Odd");
                    ++idx;
                }
                rlock.unlock();
            }
        }
    }

    class Even implements Runnable {
        @Override
        public void run() {
            while(idx < len){
                rlock.lock();
                if (idx < len && (num[idx] >> 1 << 1) == num[idx]){
                    System.out.print(num[idx]);
                    System.out.println("--Even");
                    ++idx;
                }
                rlock.unlock();
            }
        }
    }

 

对 0-99999 做三次实验耗时:

方法1:1395, 1393, 1432

方法2:1387, 1550, 1552

方法1控制台不打印:24, 24, 24

方法2控制台不打印:137, 139, 155

 

结论:

在当前实验中,synchronized 比 ReentrantLock 性能好

 

完整代码:

public class OddEven {
    int len;
    int[] num;
    int idx = 0;
    final Boolean lock = false;
    ReentrantLock rlock = new ReentrantLock();

    public OddEven(int len) {
        this.len = len;
        num = new int[len];
        for (int i = 0; i < len; i++) {
            num[i] = i;
        }
        idx = 0;
    }

    class Odd implements Runnable {
        @Override
        public void run() {
            while(idx < len){
//                synchronized (lock){
//                    if (idx < len && (num[idx] >> 1 << 1) != num[idx]){
////                        System.out.print(num[idx]);
////                        System.out.println("--Odd");
//                        ++idx;
//                    }
//                }


                rlock.lock();
                if (idx < len && (num[idx] >> 1 << 1) != num[idx]){
                    System.out.print(num[idx]);
                    System.out.println("--Odd");
                    ++idx;
                }
                rlock.unlock();
            }
        }
    }

    class Even implements Runnable {
        @Override
        public void run() {
            while(idx < len){
//                synchronized (lock){
//                    if (idx < len && (num[idx] >> 1 << 1) == num[idx]){
//                        System.out.print(num[idx]);
//                        System.out.println("--Even");
//                        ++idx;
//                    }
//                }

                rlock.lock();
                if (idx < len && (num[idx] >> 1 << 1) == num[idx]){
                    System.out.print(num[idx]);
                    System.out.println("--Even");
                    ++idx;
                }
                rlock.unlock();
            }
        }
    }

    public void execute() {

        long begin = System.currentTimeMillis();
        Thread odd = new Thread(new Odd());
        Thread even = new Thread(new Even());
        odd.start();
        even.start();

        try {
            odd.join();
            even.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
            System.out.println("被打断");
        }

        long end = System.currentTimeMillis();
        System.out.println("\ntotal time: " + (end - begin));
    }

    public static void main(String[] args) {
        OddEven oe = new OddEven(100000);
        oe.execute();
    }
}

 

java两线程交替打印奇偶数

标签:-o   span   interrupt   实验   tar   while   read   thread   static   

原文地址:https://www.cnblogs.com/GY8023/p/13553469.html

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