码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程——线程池使用示例

时间:2018-10-14 14:12:45      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:except   out   java多线程   exe   this   ted   trace   code   cep   

示例代码:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {
    public static void main(String[] args) {
        
        ExecutorService pool = Executors.newFixedThreadPool(2);
        String[] nameArr = new String[] { "孙悟空", "猪八戒", "至尊宝", "唐三藏", "沙悟净", "白晶晶", "zifeiy" };
        for (String name : nameArr) {
            pool.submit(new ThreadPoolTestWorker(name));
        }
    }
}

class ThreadPoolTestWorker implements Runnable {
    
    private String name;
    
    public ThreadPoolTestWorker(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        for (int i = 1; i <= 3; i ++) {
            System.out.println(name + " call you " + i + " times ...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
}

Java多线程——线程池使用示例

标签:except   out   java多线程   exe   this   ted   trace   code   cep   

原文地址:https://www.cnblogs.com/zifeiy/p/9785659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!