Java线程池ExecutorService和CountDownLatch的小例子

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

/**
 * 模拟了100米赛跑,10名选手已经准备就绪,只等裁判一声令下。当所有人都到达终点时,比赛结束。
 * @author liuchao
 *
 */
public class Actor {

    public static void main(String[] args) throws InterruptedException {
        //10名运动员
        final CountDownLatch count = new CountDownLatch(10);

        //java的线程池
        final ExecutorService executorService = Executors.newFixedThreadPool(5);

        for(int index=1;index<=10;index++){
            final int number = index;
            executorService.submit(new Runnable() {
                public void run() {
                    try {
                        Thread.sleep((long) (Math.random()*10000));
                        System.out.println(number+": arrived");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally{
                        //运动员到达终点,count数减一
                        count.countDown();
                    }
                }
            });
        }
        System.out.println("Game Started");
        //等待count数变为0,否则会一直处于等待状态,游戏就没法结束了
        count.await();
        System.out.println("Game Over");
        //关掉线程池
        executorService.shutdown();
    }
}

运行结果

Game Started
5: arrived
1: arrived
2: arrived
3: arrived
8: arrived
4: arrived
6: arrived
9: arrived
7: arrived
10: arrived
Game Over

时间: 2024-09-30 13:29:05

Java线程池ExecutorService和CountDownLatch的小例子的相关文章

【Java线程】Java线程池ExecutorService

示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; public class Ch09_Executor { private static void run(ExecutorService threadPool) { for(int i = 1; i < 5; i++)

Java 线程池---ExecutorService

1.线程池 1.1什么情况下使用线程池 单个任务处理的时间比较短. 将需处理的任务的数量大. 1.2使用线程池的好处 减少在创建和销毁线程上所花的时间以及系统资源的开销. 如果不使用线程池,有可能造成系统创建大量线程而导致消耗系统内存以及"过度切换"; 2.ExecutorService和Executors 2.1简介 ExecutorService是一个接口,继承了Executor, public interface ExecutorService extend Executor{

Java线程池 ExecutorService了解一下

本篇主要涉及到的是java.util.concurrent包中的ExecutorService.ExecutorService就是Java中对线程池的实现. 一.ExecutorService介绍 ExecutorService是Java中对线程池定义的一个接口,它java.util.concurrent包中,在这个接口中定义了和后台任务执行相关的方法:  Java API对ExecutorService接口的实现有两个,所以这两个即是Java线程池具体实现类: 1. ThreadPoolExe

Java 线程池ExecutorService运行原理 和FutureTask 的使用

一.线程池ExecutorService运行原理 ThreadPoolExecutor中有corePoolSize(核心线程)和maximumPoolSize(工作线程),默认核心线程和工作线程数量一致.1.当线ExecutorService线程池,使用submit,或者execute时2.先判断运行中的线程是否大于corePoolSize(核心线程)数量3.如果大于corePoolSize(核心线程)且maximumPoolSize(工作线程)未满则把该线程存着到工作线程等待.4.如果大于co

Java线程池ExecutorService

// 创建可以容纳3个线程的线程池 ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3); public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, //core pool size nThreads, //maximum pool size 0L, //keep al

转Java线程池ExecutorService

开篇前,我们先来看看不使用线程池的情况: new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? 1 2 3 4 5 6 7 new Thread(new Runnable() {     @Override     public void run() {         // TODO Auto-generated method stub     } }).start(); 那你就太out了,new Thread的弊端如下:a. 每次new Thread新建对象性能差

Java线程池 ExecutorService

java.util.concurrent包中的ExecutorService就是Java中对线程池的实现. http://blog.csdn.net/suifeng3051/article/details/49443835 原文地址:https://www.cnblogs.com/veryvalley/p/8447880.html

Java线程池:ExecutorService,Executors

简单的Java线程池可以从Executors.newFixedThreadPool( int n)获得.此方法返回一个线程容量为n的线程池.然后ExecutorService的execute执行之. 现给出一个示例. package zhangphil.executorservice; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ZhangPhil

Java中的线程池ExecutorService

示例 import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; public class Ch09_Executor { private static void run(ExecutorService threadPool) { for(int i = 1; i < 5; i++)