Java threads with returned values, preliminary use of the (cache) thread pool
I. Introduction The Thread before JDK1.5 has no return value (Thread, Runnable). The Callable interface is a new feature that will only appear later. Its usage is similar to Runnable, the difference is that there can be a return value. Therefore, in order to test the Callable class and thread pool related content, I modified the code in the previous article in a small margin and wrote the second section about the simple use of the thread pool Step 1 to define the Thread class, (1) extends Thread (2) implements Runnable (3) implements Callable <> 2. Create the ExecutorService Thread pool. For example, ExecutorService pool = Executors. newCachedThreadPool (); this is a cache pool. Common examples include: (1) newFixedThreadPool () (2) ScheduledThreadPool () (3) SingleThreadExecutor () 3. Call the thread pool to perform operations // execute tasks and obtain the Future object Future <List <String> future = pool. submit (myThread); // obtain the returned value of the task Future from the future object. get (); 3. Complete Test code
Package thread; import java. io. bufferedWriter; import java. io. file; import java. io. fileWriter; import java. io. IOException; import java. util. arrayList; import java. util. iterator; import java. util. list; import java. util. concurrent. callable; import java. util. concurrent. executionException; import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. future; public class Test2 {public static void main (String [] args) {long millis1 = System. currentTimeMillis (); String fileName = "C: \ Users \ Administrator \ Desktop \ test 2.txt"; int threadNum = 5; // List the number of threads used for testing <String> list = new ArrayList <String> (); // create a cache thread pool ExecutorService pool = Executors. newCachedThreadPool (); for (int I = 0; I <threadNum; I ++) {Callable <List <String> myThread = new MyThread2 ("Thread" + I, I, threadNum); Future <List <String> future = pool. submit (myThread); try {list. addAll (future. get ();} catch (InterruptedException e) {e. printStackTrace ();} catch (ExecutionException e) {e. printStackTrace () ;}} pool. shutdown (); // if the program is not closed, the Iterator <String> iterator = list will be terminated only after the program times out. iterator (); try {BufferedWriter writer = new BufferedWriter (new FileWriter (new File (fileName); while (iterator. hasNext () {writer. write (iterator. next (); writer. newLine (); writer. flush ();} writer. close ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} long millis2 = System. currentTimeMillis (); System. out. println (millis2-millis1); // about 170-177 ms}/*** custom task (thread) **/class MyThread2 implements Callable <List <String> {private String name; // The thread name private int I; // The number of threads private int threadNum; // Several threads are created in total: public MyThread2 (String name, int I, int threadNum) {this. name = name; this. I = I; this. threadNum = threadNum;} public List <String> call () throws Exception {MyPrint2 myPrint2 = new MyPrint2 (); return myPrint2.print (name, I, threadNum );}} /*** specific business operations **/class MyPrint2 {public List <String> print (String name, int x, int threadNum) {List <String> list = new ArrayList <String> (); for (int I = x; I <= 10000; I = I + threadNum) {// System. out. println (name + ":" + I + "------------------------------------ I am a gorgeous little tail"); list. add (name + ":" + I + "------------------------------------ I am a gorgeous little tail");} return list ;}}
Note: Due to the large number of steps, this writing method is slower than that of a single thread here ~