java web 19 : 线程池

 1 package Test01;
 2 import java.util.concurrent.ArrayBlockingQueue;
 3 import java.util.concurrent.Executors;
 4 import java.util.concurrent.ThreadPoolExecutor;
 5 import java.util.concurrent.TimeUnit;
 6 public class MyThreadPool {
 7     //两种线程模型
 8     //      用户级线程(ULT) 用户程序实现,不依赖操作系统核心,应用提供创建、同步、调度和管理线程的函数来控制用户线程。
 9     //                    不需要用户态/内核 态切换,速度快。内核对ULT无感知,线程阻塞则进程(包括它的所有线程)阻塞。
10     //      内核级线程(KLT)  系统内核管理线程(KLT),内核保存线程的状态和上下文信息,线程阻塞不会引起进程阻塞。
11     //在多处理器系统上,多线程在多处理器上并行运行。线程的创建、调度和管理由内核完成,效率比ULT要慢,比进程操作快
12     //java虚拟机使用的内核级线程(KLT)
13     //java线程创建是依赖于系统内核,通过JVM调用系统库创建内核线程,内核线程与Java-Thread是1:1的映射关系
14     //阻塞队列 1,在任意时刻,不管并发有多高,永远只有一个线程能够进行队列的入队或者出队操作!线程安全的队列
15     // 队列分有界||无界       队列满,只能进行除对操作,所有入队的操作必须等待,也就是被阻塞
16     //                      队列空,只能进行入队操作,所有出兑的操作必须等待,也就是被阻塞
17     public static void main(String[] args) {
18         ThreadPoolExecutor pool = new ThreadPoolExecutor(2,
19                 3, 60, TimeUnit.SECONDS,
20                 new ArrayBlockingQueue<Runnable>(5),Executors.defaultThreadFactory());
21         for(int i = 0;i<8;i++) {
22             //核心线程正执行任务,队列里的任务满了之后,再来的第8个任务会到临时线程中
23             //临时线程的运行顺序不一定在核心线程之后,如果再有任务,则会报异常(或者可以自己选择)
24             pool.execute(new MyTickets3(i));
25         }
26         pool.shutdown();//不再接收新任务,正在执行的线程和队列里的线程任务会执行完
27         //pool.shutdownNow();//不再执行当前的线程(线程会在安全点中断),队列里的任务也不会再执行
28     }
29 }
30 class MyTickets3 implements Runnable{
31     private int count;
32     static int tickets = 100;
33     public MyTickets3(int i) {
34         count = i;
35     }
36     @Override
37     public void run() {
38         try {
39             Thread.sleep(20);
40         } catch (InterruptedException e) {
41             e.printStackTrace();
42         }
43         //System.out.println("当前任务是任务"+count);
44         while(tickets>0) {
45                 System.out.println(Thread.currentThread().getName()+"-任务"+count+"-"+tickets--);
46         }
47     }
48 }

运行结果:

pool-1-thread-3-任务7-100
pool-1-thread-3-任务7-98
pool-1-thread-3-任务7-97
pool-1-thread-3-任务7-96
pool-1-thread-3-任务7-95
pool-1-thread-3-任务7-94
pool-1-thread-3-任务7-93
pool-1-thread-3-任务7-92
pool-1-thread-3-任务7-91
pool-1-thread-3-任务7-90
pool-1-thread-3-任务7-89
pool-1-thread-3-任务7-88
pool-1-thread-3-任务7-87
pool-1-thread-3-任务7-86
pool-1-thread-3-任务7-85
pool-1-thread-3-任务7-84
pool-1-thread-2-任务1-99
pool-1-thread-2-任务1-82
pool-1-thread-2-任务1-81
pool-1-thread-1-任务0-83
pool-1-thread-1-任务0-79
pool-1-thread-1-任务0-78
pool-1-thread-1-任务0-77
pool-1-thread-1-任务0-76
pool-1-thread-1-任务0-75
pool-1-thread-1-任务0-74
pool-1-thread-1-任务0-73
pool-1-thread-1-任务0-72
pool-1-thread-3-任务7-80
pool-1-thread-2-任务1-71
pool-1-thread-3-任务7-70
pool-1-thread-3-任务7-67
pool-1-thread-3-任务7-66
pool-1-thread-3-任务7-65
pool-1-thread-3-任务7-64
pool-1-thread-3-任务7-63
pool-1-thread-3-任务7-62
pool-1-thread-3-任务7-61
pool-1-thread-3-任务7-60
pool-1-thread-1-任务0-69
pool-1-thread-2-任务1-68
pool-1-thread-2-任务1-57
pool-1-thread-1-任务0-58
pool-1-thread-3-任务7-59
pool-1-thread-1-任务0-55
pool-1-thread-2-任务1-56
pool-1-thread-2-任务1-52
pool-1-thread-2-任务1-51
pool-1-thread-2-任务1-50
pool-1-thread-2-任务1-49
pool-1-thread-2-任务1-48
pool-1-thread-2-任务1-47
pool-1-thread-2-任务1-46
pool-1-thread-2-任务1-45
pool-1-thread-2-任务1-44
pool-1-thread-2-任务1-43
pool-1-thread-1-任务0-53
pool-1-thread-1-任务0-41
pool-1-thread-1-任务0-40
pool-1-thread-1-任务0-39
pool-1-thread-1-任务0-38
pool-1-thread-1-任务0-37
pool-1-thread-1-任务0-36
pool-1-thread-1-任务0-35
pool-1-thread-1-任务0-34
pool-1-thread-1-任务0-33
pool-1-thread-1-任务0-32
pool-1-thread-1-任务0-31
pool-1-thread-1-任务0-30
pool-1-thread-1-任务0-29
pool-1-thread-1-任务0-28
pool-1-thread-1-任务0-27
pool-1-thread-1-任务0-26
pool-1-thread-1-任务0-25
pool-1-thread-3-任务7-54
pool-1-thread-1-任务0-24
pool-1-thread-2-任务1-42
pool-1-thread-1-任务0-22
pool-1-thread-3-任务7-23
pool-1-thread-1-任务0-20
pool-1-thread-2-任务1-21
pool-1-thread-2-任务1-17
pool-1-thread-2-任务1-16
pool-1-thread-1-任务0-18
pool-1-thread-3-任务7-19
pool-1-thread-2-任务1-15
pool-1-thread-2-任务1-13
pool-1-thread-2-任务1-12
pool-1-thread-2-任务1-11
pool-1-thread-2-任务1-10
pool-1-thread-2-任务1-9
pool-1-thread-2-任务1-8
pool-1-thread-2-任务1-7
pool-1-thread-2-任务1-6
pool-1-thread-2-任务1-5
pool-1-thread-2-任务1-4
pool-1-thread-2-任务1-3
pool-1-thread-2-任务1-1
pool-1-thread-3-任务7-2
pool-1-thread-1-任务0-14

Process finished with exit code 0

 

posted @ 2020-07-24 10:28  Saturn5  阅读(26)  评论(0)    收藏  举报