zoukankan      html  css  js  c++  java
  • java两线程交替打印奇偶数

    方法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();
                }
            }
        }

    对 100000 内自然数 做三次实验耗时:

    方法1:1626, 1479, 1420

    方法2:1474, 1641, 1524

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

    方法2控制台不打印:213, 211, 211

    结论:

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

    完整代码:

    public class OddEven {
        int len;
        int[] num;
        int idx;
        final Boolean lock = false;
        ReentrantLock rlock = new ReentrantLock();
        CountDownLatch latch;
    
        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();
    
                }
    
                latch.countDown();
            }
        }
    
        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();
                }
    
                latch.countDown();
            }
        }
    
        public void execute(int threadNum) {
            latch = new CountDownLatch(threadNum);
    
            long begin = System.currentTimeMillis();
    
            Thread odd = new Thread(new Odd());
            Thread even = new Thread(new Even());
            odd.start();
            even.start();
    
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            long end = System.currentTimeMillis();
            System.out.println("
    total time: " + (end - begin));
        }
    
        public static void main(String[] args) {
            OddEven oe = new OddEven(100000);
            oe.execute(2);
        }
    }
  • 相关阅读:
    查找代码行数和查看域名版本
    iOS10里的通知与推送
    计算有多少个岛屿
    java.lang.NoClassDefFoundError: Could not initialize class com.haoyao.shop.common.XXX
    Windows 版本Mongodb 启动
    安装第三方库 报错Python version 2.7 required, which was not found in the registry
    Python 爬虫 报错 403 HTTP Error 403: Forbidden
    廖雪峰 练习 把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字
    利用Python 2.7打印杨辉三角
    MAVEN实战 读书笔记 第二章
  • 原文地址:https://www.cnblogs.com/GY8023/p/13553469.html
Copyright © 2011-2022 走看看