Thread pool-1

Subclass implementation interface:

public class ThreadPoolRunnable implements Runnable {

}

Test class:

import java. util. concurrent. ExecutorService; import java. util. concurrent. Executors; /* * JDK1. 5 to implement the thread pool program * use the static method in the factory class executors to create the thread object and specify the number of threads * static executorservice newfixedthreadpool (int): return the thread pool object * return the implementation class of the executorservice interface (thread pool object) * * interface implementation class object, Call the method submit (runnable R) to submit the thread execution task * / public class threadpooldemo {public static void main (string [] args) {/ / call the static method of the factory class to create a thread pool object. / / the thread pool object is returned, which is the returned interface executorservice es = executors. Newfixedthreadpool (2) ; // Call the interface to implement the method in the class object es and submit the thread task. / / implement the class object of the runnable interface and pass it to es submit(new ThreadPoolRunnable()); es. submit(new ThreadPoolRunnable()); es. submit(new ThreadPoolRunnable()); // Close thread pool / / es shutdown(); } }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>