Create a thread by thread pool

package thread;

import java.util.concurrent.*;

/**
* @auto dh
* @create 2020-03-29-0:04
*/
class Th0009 implements Callable<Integer>{
private int sum=0;
public Integer call(){
for(int i=1;i<=100;i++){
sum+=i;
}
return sum;
}
}
class Th009 implements Runnable {
public void run() {
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
System.out.println(Thread.currentThread().getName()+":"+i);
}

}
}
}

public class Thread009 {
static void main public (String [] args) {

Th009 Th009 new new TH = ();
Th0009 new new Th0009 T1 = ();
//. 1, to create a thread pool object by Executors tools
ExecutorService executorService = Executors.newFixedThreadPool (12);
// 2, execute () to process the Runnable interface
executorService.execute (TH);
// Submit () for processing an interface Callable
a FutureTask, a FutureTask, Future new new = (T1);
Future future1 = ExecutorService.submit (Future);

the try {
get // object by FutureTask () method returns the value of the thread
System.out.println ( "current thread return value:" + Future.get ());
} the catch (InterruptedException E) {
e.printStackTrace ();
} catch (ExecutionException e) {
e.printStackTrace ();
}
//. 3, close the thread pool
executorService.shutdown ();
}
}

Guess you like

Origin www.cnblogs.com/kukai/p/12590465.html