Java programming thread pool application, java programming thread

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.