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