Package Com.psd.test;import Java.util.arraylist;import Java.util.date;import java.util.list;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.TimeUnit; Import Org.junit.test;public class Threaddemo {//Use thread pool, length 2private final static executorservice pool = Executors.newfixe Dthreadpool (2); final long waitTime = 8 * 1000;final static Long awaittime = 5 * 1000;//for 100 numbers, turn on you two threads traverse//Define an internal threading class Countthread implements Runnable {private long a;public countthread (long a) {super (); this.a = A;} @Overridepublic void Run () {Long count = 0;for (int i = 0; i < A; i++) {count = count + i;for (int j = 0; J < A, j + + {count = count + j;}} System.err.println (Count + thread.currentthread (). GetName () + new Date ());}} @Testpublic void test02 () {//For example, to do the same thing in a packaged package//For example, there are 100 numbers to print//How to get two threads to do this thing list<runnable> runnables = new arraylist& Lt Runnable> (); Countthread runnable3 = new Countthread (100000); Countthread runnable1 = new Countthread (100000); Countthread runnable2 = new Countthread (100000); Runnables.add (Runnable3); Runnables.add (runnable1); Runnables.add ( Runnable2); for (Runnable runnable:runnables) {pool.execute (Runnable);} Pool.shutdown ();} The method tests public static void main (string[] args) {System.err.println (Thread.CurrentThread (). GetName () + new Date ()); Threaddemo Threaddemo = new Threaddemo (); threaddemo.test02 ();} public void test03 () {this.test04 (100000); this.test04 (100000); this.test04 (100000);} public void test04 (long a) {Long Count = 0;for (int i = 0; i < A; i++) {count = count + i;for (int j = 0; J < A, j + + {count = count + j;}} System.err.println (count);}}
To turn on two thread traversal using a thread pool