Think_in_java_4th(并发学习二)

Posted 匆匆而过

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Think_in_java_4th(并发学习二)相关的知识,希望对你有一定的参考价值。

使用Executor

java.util.concurrent

CachedThreadPool

package concurrency.ExecutorService;

//: concurrency/CachedThreadPool.java
import java.util.concurrent.*;

import concurrency.LiftOff.LiftOff;

public class CachedThreadPool {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++)
            exec.execute(new LiftOff());
        exec.shutdown();
    }
} /*
   * Output: (Sample) #0(9), #0(8), #1(9), #2(9), #3(9), #4(9), #0(7), #1(8),
   * #2(8), #3(8), #4(8), #0(6), #1(7), #2(7), #3(7), #4(7), #0(5), #1(6), #2(6),
   * #3(6), #4(6), #0(4), #1(5), #2(5), #3(5), #4(5), #0(3), #1(4), #2(4), #3(4),
   * #4(4), #0(2), #1(3), #2(3), #3(3), #4(3), #0(1), #1(2), #2(2), #3(2), #4(2),
   * #0(Liftoff!), #1(1), #2(1), #3(1), #4(1), #1(Liftoff!), #2(Liftoff!),
   * #3(Liftoff!), #4(Liftoff!),
   */// :~

FixedThreadPool

package concurrency.ExecutorService;

//: concurrency/FixedThreadPool.java
import java.util.concurrent.*;

import concurrency.LiftOff.LiftOff;

public class FixedThreadPool {
    public static void main(String[] args) {
        // Constructor argument is number of threads:
        ExecutorService exec = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++)
            exec.execute(new LiftOff());
        exec.shutdown();
    }
} /*
   * Output: (Sample) #0(9), #0(8), #1(9), #2(9), #3(9), #4(9), #0(7), #1(8),
   * #2(8), #3(8), #4(8), #0(6), #1(7), #2(7), #3(7), #4(7), #0(5), #1(6), #2(6),
   * #3(6), #4(6), #0(4), #1(5), #2(5), #3(5), #4(5), #0(3), #1(4), #2(4), #3(4),
   * #4(4), #0(2), #1(3), #2(3), #3(3), #4(3), #0(1), #1(2), #2(2), #3(2), #4(2),
   * #0(Liftoff!), #1(1), #2(1), #3(1), #4(1), #1(Liftoff!), #2(Liftoff!),
   * #3(Liftoff!), #4(Liftoff!),
   */// :~

SingleThreadExecutor

package concurrency.ExecutorService;

//: concurrency/SingleThreadExecutor.java
import java.util.concurrent.*;

import concurrency.LiftOff.LiftOff;

public class SingleThreadExecutor {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 5; i++)
            exec.execute(new LiftOff());
        exec.shutdown();
    }
} /*
   * Output: #0(9), #0(8), #0(7), #0(6), #0(5), #0(4), #0(3), #0(2), #0(1),
   * #0(Liftoff!), #1(9), #1(8), #1(7), #1(6), #1(5), #1(4), #1(3), #1(2), #1(1),
   * #1(Liftoff!), #2(9), #2(8), #2(7), #2(6), #2(5), #2(4), #2(3), #2(2), #2(1),
   * #2(Liftoff!), #3(9), #3(8), #3(7), #3(6), #3(5), #3(4), #3(3), #3(2), #3(1),
   * #3(Liftoff!), #4(9), #4(8), #4(7), #4(6), #4(5), #4(4), #4(3), #4(2), #4(1),
   * #4(Liftoff!),
   */// :~

参考

Java编程思想(第4版)    656页开始

Thinking in Java(第四版 )  1120页开始

以上是关于Think_in_java_4th(并发学习二)的主要内容,如果未能解决你的问题,请参考以下文章

软件工程_4th weeks

FPGA学习笔记03-VHDL语法基础-信号代入语句

《algorithm_4th》 1 chapter:Fundamentals

LeetCode_22_Apr_4th_Week

面试通天代!字节大牛给新入职带来的“高并发系统设计”学习笔记,已帮助他实现职业三连升!

JVM19_G1垃圾收集器概述特点常用参数Region详解记忆集与写屏障年轻代GC并发标记过程Mixed GCFull GC