這里簡單說一個線程池的小應(yīng)用,從控制臺輸入線程訪問的線程數(shù)量,輸出輸出工作的線程名,之前先構(gòu)造一個包含了5條線程的對象。
代碼:
import java.util.Scanner;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Threadd { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); ExecutorService es = Executors.newFixedThreadPool(5);//初始化五條線程,當(dāng)使用的時候就使用,沒有訪問就掛起 runn r = new runn(); int i = Integer.parseInt(s.nextLine());//此處從控制臺輸入工作的線程數(shù)量 while(--i>0){ es.execute(r); } while(true){ if(es.isTerminated()){ System.out. 輸入:7
輸出:pool-1-thread-1正在運(yùn)行 pool-1-thread-3正在運(yùn)行 pool-1-thread-2正在運(yùn)行 pool-1-thread-4正在運(yùn)行 pool-1-thread-2正在運(yùn)行 pool-1-thread-5正在運(yùn)行
新聞熱點(diǎn)
疑難解答
圖片精選