zoukankan      html  css  js  c++  java
  • java_线程-锁

    package com.demo.test3;
    
    import java.util.concurrent.CountDownLatch;
    
    /**
     * @author QQ: 1236897
     *
     */
    //闭锁
    //nThread - 线程数目
    //startGate -确保所有线程就绪-》countDown->所有线程工作
    //endGate - 等待所有线程完成工作后才返回timeTask方法
    public class CountDownLockTest {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Caller caller = new Caller();
            MyTask task = new MyTask();
    
            try {
                System.out.println(caller.timeTask(5, task));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
    }
    
    class Caller {
    
        public long timeTask(int nThreads, final Runnable task)
                throws InterruptedException {
    
            final CountDownLatch startGate = new CountDownLatch(1);
            final CountDownLatch endGate = new CountDownLatch(nThreads);
    
            for (int i = 0; i < nThreads; i++) {
                Thread t = new Thread() {
    
                    public void run() {
                        try {
                            System.out.println("startGate await");
                            startGate.await();
                            try {
                                task.run();
                            } finally {
                                endGate.countDown();
                            }
    
                        } catch (InterruptedException e) {
                        }
                    }
    
                };
    
                t.start();
            }
    
            long start = System.nanoTime();
            System.out.println("startGate countDown");
            startGate.countDown();
            endGate.await();
            long end = System.nanoTime();
            System.out.println("return");
            return end - start;
        }
    
    }
    
    class MyTask implements Runnable {
    
        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Runnable#run()
         */
        @Override
        public void run() {
            // TODO Auto-generated method stub
            System.out.println("id sleep: - " + Thread.currentThread().getId());
            try {
                Thread.sleep(5000);
                System.out.println("Sleep done: - "
                        + Thread.currentThread().getId());
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                Thread.currentThread().interrupt();
            }
        }
    
    }
  • 相关阅读:
    经常使用排序算法时间复杂度和空间复杂度简析
    Android触碰事件
    opencv矩阵运算(2)
    [ACM] HDU 1400 Mondriaan&#39;s Dream (状态压缩,长2宽1长方形铺满)
    指针知识梳理8- 指针的指针
    Git学习笔记(一)
    Object-c Associated Object
    YTUOJ-计算该日在本年中是第几天(用户自己定义类型)
    MYSQL源代码编译的变动
    手机端小问题整理
  • 原文地址:https://www.cnblogs.com/MarchThree/p/4769858.html
Copyright © 2011-2022 走看看