Countdownlatch thread starts multiple threads and counts the time that all threads consume

Source: Internet
Author: User

simulates starting 3,000 threads, concurrently executing and counting the time it takes for all threads to finish executing.

Import Java.util.HashMap;
Import Java.util.Map;

Import Java.util.concurrent.CountDownLatch; public class Threadhttps {public static void main (string[] args) {try {long start = System.currenttimemillis (
			); Countdownlatch startlatch=new Countdownlatch (300);//How many threads need to wait to start Countdownlatch endlatch=new Countdownlatch (300);// How many threads need to wait to finish after for (int i =0;i<300;i++) {map<string, string> Map = new hashmap<string, string&
	        	gt; ();
	    		Start both threads and go to call BS20102 MyThread myThread1 = new MyThread ("" +i,map,startlatch,endlatch);
	    		Mythread1.start ();
	        Startlatch.countdown ();
	        } endlatch.await ();//wait for all workers to complete the work long end = System.currenttimemillis (); 
		System.out.println ("Time spent on all work completed:" + (End-start) + "milliseconds");
		} catch (Exception e) {e.printstacktrace ();
			}} public static class MyThread extends thread{private String tradetype;
			Private map<string, string> Map; Private Countdownlatch Startlatch;
			Private Countdownlatch Endlatch; Public MyThread (String tradetype,map<string, string> Map, Countdownlatch startlatch,countdownlatch endlatch) {th
				Is.tradetype =tradetype;
				This.map = map;
				This.startlatch=startlatch;
			This.endlatch=endlatch;
										
				} public void Run () {System.out.println ("Latch.getcount:" + startlatch.getcount ());
					try {while (Startlatch.getcount ()! = 0) {thread.sleep (10);
					} Long start = System.currenttimemillis ();
					System.out.println ("Tradetype:" + Tradetype + ", ~ ~ ~ Start Execution ~ ~ ~");
					Thread.Sleep (3000);
					Long end = System.currenttimemillis ();
					
					System.out.println ("Tradetype:" +tradetype+ ", Thread Consumption time" + (End-start));
					Endlatch.countdown ();//After the thread content is executed, the counter is reduced by one} catch (Interruptedexception e) {//TODO auto-generated catch block
				E.printstacktrace ();
 }			
			}
		}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.