Java programming thread pool application, java programming thread
Here is a simple example of a small application in a thread pool. The number of threads accessed by the input thread in the console and the name of the output thread are constructed. An object containing five threads is constructed before.
Code:
Import java. util. imports; import java. util. concurrent. executorService; import java. util. concurrent. executors; public class Threadd {public static void main (String [] args) {// TODO Auto-generated method stub s = new partition (System. in); ExecutorService es = Executors. newFixedThreadPool (5); // initialize five threads and use them when using them. If no access is available, the runn r = new runn (); int I = Integer will be suspended. parseInt (s. nextLine (); // enter the number of working threads from the console. While (-- I> 0) {es.exe cute (r);} while (true) {if (es. isTerminated () {System. out. println ("All thread tasks have been completed"); break ;}} es. shutdown (); try {Thread. sleep (1000); if (es. isShutdown () {System. out. println ("the thread pool has been destroyed. ") ;}} Catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} class runn implements Runnable {@ Override public void run () {// TODO Auto-generated method stub System. out. println (Thread. currentThread (). getName () + "running ");}}
Input: 7
Output: pool-1-thread-1 is running
Pool-1-thread-3 is running
Pool-1-thread-2 is running
Pool-1-thread-4 is running
Pool-1-thread-2 is running
Pool-1-thread-5 is running