zoukankan      html  css  js  c++  java
  • 闭锁

      延迟线程的进度,等待闭锁条件的结束

    import java.io.*;
    import java.security.NoSuchAlgorithmException;
    import java.text.ParseException;
    import java.util.concurrent.CountDownLatch;
    
    public class Main
    {
    
    	final static CountDownLatch start = new CountDownLatch(1);
    	final static CountDownLatch end = new CountDownLatch(5);
    
    	public static void main(String[] args) throws FileNotFoundException,
    			NoSuchAlgorithmException, ParseException, InterruptedException
    	{
    		long t1 = System.currentTimeMillis();
    		for (int i = 0; i < 5; i++)
    		{
    			new Thread(new MyRunable(i)).start();
    		}
    		start.countDown();
    		end.await();
    		// long total = Integer.MAX_VALUE*5;
    		// for(long i = 0; i < total;i++);
    		long t2 = System.currentTimeMillis();
    		System.out.println("total time=" + (t2 - t1));
    		System.out.println("total time=" + (t2 - t1) / 1000);
    	}
    
    	static class MyRunable implements Runnable
    	{
    		int i;
    
    		public MyRunable(int i)
    		{
    			this.i = i;
    		}
    
    		public void run()
    		{
    			try
    			{
    				start.await();
    			}
    			catch (InterruptedException e)
    			{
    				e.printStackTrace();
    			}
    			for (int i = 0; i < Integer.MAX_VALUE; i++)
    				if (i == 100)
    				{
    					System.out.println(i + " is 100");
    				}
    			System.out.println(i + " is end");
    			end.countDown();
    		}
    	}
    }
    

      

  • 相关阅读:
    React 进修之路(1)
    requireJS的配置心得
    DOM的内部插入和外部插入
    h5移动端设备像素比dpr介绍
    原生js--事件类型
    React 进修之路(3)
    javaScript
    html&css
    MyBatis
    Maven基础
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6841540.html
Copyright © 2011-2022 走看看