Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java thread pool: ExecutorService, Executors     - Python type way of comparison (Programming)

- Ubuntu 14.10 used ifconfig commands to manage your network configuration (Linux)

- How to use Android Studio development / debugging Android source code (Linux)

- How to use the command line to obtain Freely RSS source on Linux (Linux)

- Linux rights management (Linux)

- Summary Linux bond of multi-interface load balancing (Linux)

- How to implement large-scale distributed Yahoo depth study on the Hadoop cluster (Server)

- Four safety delete files under Linux tools (Linux)

- AngularJS notes --- Data Binding (Programming)

- File sharing and fork function (Programming)

- Oracle how to maintain the consistency of read? (Database)

- namespace mechanism Linux kernel analysis (Linux)

- To install Git on Ubuntu systems (Server)

- grep command Detailed and relevant examples (Linux)

- Ant command-line compiler Android project (Programming)

- How to properly set up a Linux swap partition (Linux)

- Linux and Windows Security Topics (Linux)

- Install Debian Linux with R on the Android system (Linux)

- C ++ Supplements - malloc free and new delete the same and different (Programming)

- Python basis: os module on the file / directory using methods commonly used functions (Programming)

 
         
  Java thread pool: ExecutorService, Executors
     
  Add Date : 2018-11-21      
         
         
         
  A simple Java thread pool can be obtained from Executors.newFixedThreadPool (int n). This method returns a thread pool with thread capacity n. ExecutorService then the implementation of the execute.

An example is given.

Package zhangphil.executorservice;

Import java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;

Public class ZhangPhilExecutorService {

 / / In order to easily understand the concept of thread pool, assuming the capacity of only 2 thread pool.
 // The actual course of the course can be more!
 Private final int NUMBER = 2;

 Public ZhangPhilExecutorService () {
  / / Create a capacity of 2 thread pool.
  ExecutorService pool = Executors.newFixedThreadPool (NUMBER);

  For (int i = 0; i < 10; i ++) {
   Thread t = new TestThread (i);
   System.out.println ( "thread pool execution thread id:" + i);
   Pool.execute (t);
  }}

  // Close the thread pool.
  Pool.shutdown ();
 }}

 Private class TestThread extends Thread {

  Private int id;

  Public TestThread (int id) {
   This.id = id;
  }}

  @Override
  Public void run () {

   System.out.println ( "thread:" + id + "-> run ...");

   Try {
    Thread.sleep (5000);
   } Catch (Exception e) {
    E.printStackTrace ();
   }}

   System.out.println ( "thread:" + id + "-> end!");
  }}
 }}

 Public static void main (String [] args) {
  New ZhangPhilExecutorService ();
 }}
}}

Run the output:

Thread pool execution thread id: 0
Thread pool execution thread id: 1
Thread pool execution thread id: 2
Thread pool execution thread id: 3
Thread: 0 -> run ...
Thread pool execution thread id: 4
Thread: 1 -> Run ...
Thread pool execution thread id: 5
Thread pool execution thread id: 6
Thread pool execution thread id: 7
Thread pool execution thread id: 8
Thread pool execution thread id: 9
Thread: 1 -> End!
Thread: 0 -> End!
Thread: 2 -> run ...
Thread: 3 -> run ...
Thread: 3 -> End!
Thread: 2 -> End!
Thread: 4 -> Run ...
Thread: 5 -> run ...
Thread: 4 -> End!
Thread: 5 -> End!
Thread: 6 -> run ...
Thread: 7 -> Run ...
Thread: 7 -> End!
Thread: 6 -> End!
Thread: 8 -> run ...
Thread: 9 -> Run ...
Thread: 9 -> End!
Thread: 8 -> End!
     
         
         
         
  More:      
 
- Differential test piece using MongoDB performance YCSB (Database)
- JavaScript function definition mode (Programming)
- Ruby and Python simple comparison (Programming)
- Kubernetes Cluster Setup problems encountered and solutions (Server)
- Python Basics Tutorial - lambda keyword (Programming)
- Iptables use examples (Linux)
- Deploy OpenStack Juno on Ubuntu 14.04 (Linux)
- Linux operating system security can not be ignored (Linux)
- HDFS Distributed File System Resource Manager Developer summary (Server)
- C # Future: Method Contract (Programming)
- Linux system security (Linux)
- 20 Linux commands interview questions and answers (Linux)
- Ubuntu 14.10 splash screen brightness settings (Linux)
- JDK installation under CentOS (Linux)
- 10 Regulation of painless SQL Schema (Database)
- Chkconfig set boot start under Linux (Linux)
- OS X CAOpenGLLayer how to enable OpenGL 3.2 core profile (Programming)
- CentOS 6.7 install Nagios Tutorials (Server)
- Linux system started to learn: Teaches you install Fedora 22 on VirtualBox (Linux)
- About DataGuard three protected mode test (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.