Executor -- executor of the thread, Executor -- thread execution
The Executor in the Java. util. concurrent package of java SE5 is used to manage Thread objects and simplify concurrent programming.Light-off thread:
public class LiftOffThread implements Runnable { protected int countDown = 10; private static int taskCount = 0; private final int id = taskCount++; public LiftOffThread() { } public String status() { return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff") + ")"; } @Override public void run() { while (countDown-- > 0) { System.out.println(status()); } }}
Use Excutor 1:
ExecutorService mService = Executors.newCachedThreadPool(); for (int i = 0; i < 5; i++) { mService.execute(new LiftOffThread()); } mService.shutdown();
Running result:
Use Excutor 2:
// Number of pre-allocated threads ExecutorService mService = Executors. newFixedThreadPool (3); for (int I = 0; I <5; I ++) {mService.exe cute (new LiftOffThread ();} mService. shutdown ();
Running result:
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.