zoukankan      html  css  js  c++  java
  • java 5 线程池

     1 import java.util.concurrent.ExecutorService;
     2 import java.util.concurrent.Executors;
     3 
     4 public class ThreadPoolTest
     5 {
     6     public static void main(String[] args)
     7     {
     8         ExecutorService threadPool = Executors.newFixedThreadPool(3);
     9         
    10         for(int i = 0; i < 10; i++)
    11         {
    12             final String taskName = "task " + i;
    13             threadPool.execute(new Runnable()
    14             {
    15                 public void run()
    16                 {
    17                     System.out.println(Thread.currentThread().getName() + "-" + taskName);
    18                 }
    19             });
    20         }
    threadPool.shutdown();
    21 } 22 }

    输出结果:

    pool-1-thread-2-task 1
    pool-1-thread-1-task 0
    pool-1-thread-3-task 2
    pool-1-thread-2-task 3
    pool-1-thread-1-task 4
    pool-1-thread-2-task 6
    pool-1-thread-3-task 5
    pool-1-thread-2-task 8
    pool-1-thread-1-task 7
    pool-1-thread-3-task 9

  • 相关阅读:
    mtu
    OC2_使用系统协议
    OC1_协议语句
    Json文件/网址解析
    文件归档
    Plist文件
    NS-Date/NSDateFormatter
    OC10_文件练习
    OC9_文件操作
    OC8_NSData
  • 原文地址:https://www.cnblogs.com/java-koma/p/4052638.html
Copyright © 2011-2022 走看看