Use Callable interface to create a thread pool

step:

Create a thread pool object
is created Callable interface sub-class object
submitted Callable interface sub-class objects
close the thread pool
instance:

class TaskCallable implements Callable<Integer> {

@Override
public Integer call() throws Exception {
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName()+" "+i);
}
return null;
}
}
public class ThreadPoolOfCallable {

public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(3);
TaskCallable c = new TaskCallable();
TaskCallable c1 = new TaskCallable();

// Get the thread pool thread object, calling run
service.submit (C);
// reacquisition a
service.submit (C1);
// Close thread pool
service.shutdown ();
}

}

---------------------

Guess you like

Origin www.cnblogs.com/liyanyan665/p/11359760.html