线程高级应用-心得4-java5线程并发库介绍,及新技术案例分析
1. java5线程并发库新知识介绍
2.线程并发库案例分析
- package com.itcast.family;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- public class ThreadPoolTest {
- /**
- * @param args
- */
- public static void main(String[] args) {
- //1.new数量为3的固定的线程池;工具类一般都带着s;
- //ExecutorService threadPool = Executors.newFixedThreadPool(3);
- //2.缓存线程池,好处:需要几个线程就创建几个;不创建多余也不少创建
- //ExecutorService threadPool = Executors.newCachedThreadPool();
- //单例线程,就好比单独创建一个线程;好处:该线程死了立刻又创建出一个,可以解决线程死后重新启动的问题
- ExecutorService threadPool = Executors.newSingleThreadExecutor();
- for (int i = 1; i <= 10; i++) {
- // 匿名内部类中不能必须使用最终变量;
- final int task = i;
- threadPool.execute(new Runnable() {
- @Override
- public void run() {
- for (int j = 1; j <= 10; j++) {
- try {
- Thread.sleep(20);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println(Thread.currentThread().getName()+" is looping of "+j+" for task of "+task);
- }
- }
- });
- }
- System.out.println("all of 10 takes have committed!!");
- //线程池中没有任务了就结束线程池,如果不使用就会就算线程池中所有任务都结束了但是线程还在等着运行,不结束
- //threadPool.shutdown();
- /*使用shutdown()方法的巡行结果:
- * 分析下面结果发现循环了10次即这个任务都调用的线程池中的这三个线程,每个任务又重复执行10次
- all of 10 takes have committed!!
- pool-1-thread-1 is looping of 1 for task of 1
- pool-1-thread-3 is looping of 1 for task of 3
- pool-1-thread-2 is looping of 1 for task of 2
- pool-1-thread-3 is looping of 2 for task of 3
- pool-1-thread-1 is looping of 2 for task of 1
- pool-1-thread-2 is looping of 2 for task of 2
- pool-1-thread-3 is looping of 3 for task of 3
- pool-1-thread-1 is looping of 3 for task of 1
- pool-1-thread-2 is looping of 3 for task of 2
- pool-1-thread-3 is looping of 4 for task of 3
- pool-1-thread-1 is looping of 4 for task of 1
- pool-1-thread-2 is looping of 4 for task of 2
- pool-1-thread-1 is looping of 5 for task of 1
- pool-1-thread-3 is looping of 5 for task of 3
- pool-1-thread-2 is looping of 5 for task of 2
- pool-1-thread-1 is looping of 6 for task of 1
- pool-1-thread-3 is looping of 6 for task of 3
- pool-1-thread-2 is looping of 6 for task of 2
- pool-1-thread-3 is looping of 7 for task of 3
- pool-1-thread-1 is looping of 7 for task of 1
- pool-1-thread-2 is looping of 7 for task of 2
- pool-1-thread-1 is looping of 8 for task of 1
- pool-1-thread-3 is looping of 8 for task of 3
- pool-1-thread-2 is looping of 8 for task of 2
- pool-1-thread-1 is looping of 9 for task of 1
- pool-1-thread-3 is looping of 9 for task of 3
- pool-1-thread-2 is looping of 9 for task of 2
- pool-1-thread-1 is looping of 10 for task of 1
- pool-1-thread-3 is looping of 10 for task of 3
- pool-1-thread-2 is looping of 10 for task of 2
- pool-1-thread-3 is looping of 1 for task of 5
- pool-1-thread-1 is looping of 1 for task of 4
- pool-1-thread-2 is looping of 1 for task of 6
- pool-1-thread-3 is looping of 2 for task of 5
- pool-1-thread-1 is looping of 2 for task of 4
- pool-1-thread-2 is looping of 2 for task of 6
- pool-1-thread-1 is looping of 3 for task of 4
- pool-1-thread-3 is looping of 3 for task of 5
- pool-1-thread-2 is looping of 3 for task of 6
- pool-1-thread-1 is looping of 4 for task of 4
- pool-1-thread-3 is looping of 4 for task of 5
- pool-1-thread-2 is looping of 4 for task of 6
- pool-1-thread-1 is looping of 5 for task of 4
- pool-1-thread-3 is looping of 5 for task of 5
- pool-1-thread-2 is looping of 5 for task of 6
- pool-1-thread-1 is looping of 6 for task of 4
- pool-1-thread-3 is looping of 6 for task of 5
- pool-1-thread-2 is looping of 6 for task of 6
- pool-1-thread-1 is looping of 7 for task of 4
- pool-1-thread-3 is looping of 7 for task of 5
- pool-1-thread-2 is looping of 7 for task of 6
- pool-1-thread-3 is looping of 8 for task of 5
- pool-1-thread-1 is looping of 8 for task of 4
- pool-1-thread-2 is looping of 8 for task of 6
- pool-1-thread-1 is looping of 9 for task of 4
- pool-1-thread-3 is looping of 9 for task of 5
- pool-1-thread-2 is looping of 9 for task of 6
- pool-1-thread-3 is looping of 10 for task of 5
- pool-1-thread-1 is looping of 10 for task of 4
- pool-1-thread-2 is looping of 10 for task of 6
- pool-1-thread-1 is looping of 1 for task of 8
- pool-1-thread-3 is looping of 1 for task of 7
- pool-1-thread-2 is looping of 1 for task of 9
- pool-1-thread-3 is looping of 2 for task of 7
- pool-1-thread-1 is looping of 2 for task of 8
- pool-1-thread-2 is looping of 2 for task of 9
- pool-1-thread-1 is looping of 3 for task of 8
- pool-1-thread-3 is looping of 3 for task of 7
- pool-1-thread-2 is looping of 3 for task of 9
- pool-1-thread-1 is looping of 4 for task of 8
- pool-1-thread-3 is looping of 4 for task of 7
- pool-1-thread-2 is looping of 4 for task of 9
- pool-1-thread-1 is looping of 5 for task of 8
- pool-1-thread-3 is looping of 5 for task of 7
- pool-1-thread-2 is looping of 5 for task of 9
- pool-1-thread-1 is looping of 6 for task of 8
- pool-1-thread-3 is looping of 6 for task of 7
- pool-1-thread-2 is looping of 6 for task of 9
- pool-1-thread-1 is looping of 7 for task of 8
- pool-1-thread-3 is looping of 7 for task of 7
- pool-1-thread-2 is looping of 7 for task of 9
- pool-1-thread-3 is looping of 8 for task of 7
- pool-1-thread-1 is looping of 8 for task of 8
- pool-1-thread-2 is looping of 8 for task of 9
- pool-1-thread-3 is looping of 9 for task of 7
- pool-1-thread-1 is looping of 9 for task of 8
- pool-1-thread-2 is looping of 9 for task of 9
- pool-1-thread-1 is looping of 10 for task of 8
- pool-1-thread-3 is looping of 10 for task of 7
- pool-1-thread-2 is looping of 10 for task of 9
- pool-1-thread-1 is looping of 1 for task of 10
- pool-1-thread-1 is looping of 2 for task of 10
- pool-1-thread-1 is looping of 3 for task of 10
- pool-1-thread-1 is looping of 4 for task of 10
- pool-1-thread-1 is looping of 5 for task of 10
- pool-1-thread-1 is looping of 6 for task of 10
- pool-1-thread-1 is looping of 7 for task of 10
- pool-1-thread-1 is looping of 8 for task of 10
- pool-1-thread-1 is looping of 9 for task of 10
- pool-1-thread-1 is looping of 10 for task of 10*/
- //结束当前线程
- // threadPool.shutdownNow();
- /*
- * 运行结果:
- * 主要看下面的结果,暂且忽略错误提示;上面的是十个线程,这个却是三个,
- * 这是因为结束当前线程,当前就三个线程。所以直接结束了不管后面没有线程的
- * 其他七个任务。
- * all of 10 takes have committed!!
- java.lang.InterruptedException: sleep interrupted
- at java.lang.Thread.sleep(Native Method)
- at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:619)
- pool-1-thread-1 is looping of 1 for task of 1java.lang.InterruptedException: sleep interrupted
- at java.lang.Thread.sleep(Native Method)
- at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:619)
- pool-1-thread-2 is looping of 1 for task of 2
- java.lang.InterruptedException: sleep interrupted
- at java.lang.Thread.sleep(Native Method)
- at com.itcast.family.ThreadPoolTest$1.run(ThreadPoolTest.java:25)
- at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
- at java.lang.Thread.run(Thread.java:619)
- pool-1-thread-3 is looping of 1 for task of 3
- pool-1-thread-1 is looping of 2 for task of 1
- pool-1-thread-2 is looping of 2 for task of 2
- pool-1-thread-3 is looping of 2 for task of 3
- pool-1-thread-2 is looping of 3 for task of 2
- pool-1-thread-1 is looping of 3 for task of 1
- pool-1-thread-3 is looping of 3 for task of 3
- pool-1-thread-1 is looping of 4 for task of 1
- pool-1-thread-2 is looping of 4 for task of 2
- pool-1-thread-3 is looping of 4 for task of 3
- pool-1-thread-2 is looping of 5 for task of 2
- pool-1-thread-1 is looping of 5 for task of 1
- pool-1-thread-3 is looping of 5 for task of 3
- pool-1-thread-1 is looping of 6 for task of 1
- pool-1-thread-2 is looping of 6 for task of 2
- pool-1-thread-3 is looping of 6 for task of 3
- pool-1-thread-2 is looping of 7 for task of 2
- pool-1-thread-1 is looping of 7 for task of 1
- pool-1-thread-3 is looping of 7 for task of 3
- pool-1-thread-2 is looping of 8 for task of 2
- pool-1-thread-1 is looping of 8 for task of 1
- pool-1-thread-3 is looping of 8 for task of 3
- pool-1-thread-1 is looping of 9 for task of 1
- pool-1-thread-2 is looping of 9 for task of 2
- pool-1-thread-3 is looping of 9 for task of 3
- pool-1-thread-2 is looping of 10 for task of 2
- pool-1-thread-1 is looping of 10 for task of 1
- pool-1-thread-3 is looping of 10 for task of 3
- */
- /*动态变化即缓存那个的运行结果:
- * 需要几个线程就new出几个线程;这里是个是个任务就new出了十个线程
- * 而不是像第一种那样是个任务三个线程
- * all of 10 takes have committed!!
- pool-1-thread-2 is looping of 1 for task of 2
- pool-1-thread-4 is looping of 1 for task of 4
- pool-1-thread-6 is looping of 1 for task of 6
- pool-1-thread-8 is looping of 1 for task of 8
- pool-1-thread-10 is looping of 1 for task of 10
- pool-1-thread-1 is looping of 1 for task of 1
- pool-1-thread-3 is looping of 1 for task of 3
- pool-1-thread-5 is looping of 1 for task of 5
- pool-1-thread-7 is looping of 1 for task of 7
- pool-1-thread-9 is looping of 1 for task of 9
- pool-1-thread-6 is looping of 2 for task of 6
- pool-1-thread-4 is looping of 2 for task of 4
- pool-1-thread-2 is looping of 2 for task of 2
- pool-1-thread-1 is looping of 2 for task of 1
- pool-1-thread-8 is looping of 2 for task of 8
- pool-1-thread-10 is looping of 2 for task of 10
- pool-1-thread-3 is looping of 2 for task of 3
- pool-1-thread-5 is looping of 2 for task of 5
- pool-1-thread-7 is looping of 2 for task of 7
- pool-1-thread-9 is looping of 2 for task of 9
- pool-1-thread-6 is looping of 3 for task of 6
- pool-1-thread-4 is looping of 3 for task of 4
- pool-1-thread-1 is looping of 3 for task of 1
- pool-1-thread-2 is looping of 3 for task of 2
- pool-1-thread-8 is looping of 3 for task of 8
- pool-1-thread-5 is looping of 3 for task of 5
- pool-1-thread-3 is looping of 3 for task of 3
- pool-1-thread-10 is looping of 3 for task of 10
- pool-1-thread-9 is looping of 3 for task of 9
- pool-1-thread-7 is looping of 3 for task of 7
- pool-1-thread-6 is looping of 4 for task of 6
- pool-1-thread-1 is looping of 4 for task of 1
- pool-1-thread-4 is looping of 4 for task of 4
- pool-1-thread-8 is looping of 4 for task of 8
- pool-1-thread-2 is looping of 4 for task of 2
- pool-1-thread-3 is looping of 4 for task of 3
- pool-1-thread-5 is looping of 4 for task of 5
- pool-1-thread-10 is looping of 4 for task of 10
- pool-1-thread-7 is looping of 4 for task of 7
- pool-1-thread-9 is looping of 4 for task of 9
- pool-1-thread-1 is looping of 5 for task of 1
- pool-1-thread-6 is looping of 5 for task of 6
- pool-1-thread-4 is looping of 5 for task of 4
- pool-1-thread-2 is looping of 5 for task of 2
- pool-1-thread-5 is looping of 5 for task of 5
- pool-1-thread-3 is looping of 5 for task of 3
- pool-1-thread-8 is looping of 5 for task of 8
- pool-1-thread-10 is looping of 5 for task of 10
- pool-1-thread-7 is looping of 5 for task of 7
- pool-1-thread-9 is looping of 5 for task of 9
- pool-1-thread-1 is looping of 6 for task of 1
- pool-1-thread-6 is looping of 6 for task of 6
- pool-1-thread-4 is looping of 6 for task of 4
- pool-1-thread-5 is looping of 6 for task of 5
- pool-1-thread-3 is looping of 6 for task of 3
- pool-1-thread-2 is looping of 6 for task of 2
- pool-1-thread-10 is looping of 6 for task of 10
- pool-1-thread-8 is looping of 6 for task of 8
- pool-1-thread-7 is looping of 6 for task of 7
- pool-1-thread-9 is looping of 6 for task of 9
- pool-1-thread-1 is looping of 7 for task of 1
- pool-1-thread-4 is looping of 7 for task of 4
- pool-1-thread-6 is looping of 7 for task of 6
- pool-1-thread-2 is looping of 7 for task of 2
- pool-1-thread-3 is looping of 7 for task of 3
- pool-1-thread-5 is looping of 7 for task of 5
- pool-1-thread-8 is looping of 7 for task of 8
- pool-1-thread-10 is looping of 7 for task of 10
- pool-1-thread-7 is looping of 7 for task of 7
- pool-1-thread-9 is looping of 7 for task of 9
- pool-1-thread-1 is looping of 8 for task of 1
- pool-1-thread-6 is looping of 8 for task of 6
- pool-1-thread-4 is looping of 8 for task of 4
- pool-1-thread-2 is looping of 8 for task of 2
- pool-1-thread-5 is looping of 8 for task of 5
- pool-1-thread-3 is looping of 8 for task of 3
- pool-1-thread-8 is looping of 8 for task of 8
- pool-1-thread-10 is looping of 8 for task of 10
- pool-1-thread-7 is looping of 8 for task of 7
- pool-1-thread-9 is looping of 8 for task of 9
- pool-1-thread-1 is looping of 9 for task of 1
- pool-1-thread-6 is looping of 9 for task of 6
- pool-1-thread-4 is looping of 9 for task of 4
- pool-1-thread-5 is looping of 9 for task of 5
- pool-1-thread-3 is looping of 9 for task of 3
- pool-1-thread-2 is looping of 9 for task of 2
- pool-1-thread-8 is looping of 9 for task of 8
- pool-1-thread-10 is looping of 9 for task of 10
- pool-1-thread-7 is looping of 9 for task of 7
- pool-1-thread-9 is looping of 9 for task of 9
- pool-1-thread-1 is looping of 10 for task of 1
- pool-1-thread-3 is looping of 10 for task of 3
- pool-1-thread-5 is looping of 10 for task of 5
- pool-1-thread-2 is looping of 10 for task of 2
- pool-1-thread-4 is looping of 10 for task of 4
- pool-1-thread-6 is looping of 10 for task of 6
- pool-1-thread-8 is looping of 10 for task of 8
- pool-1-thread-7 is looping of 10 for task of 7
- pool-1-thread-9 is looping of 10 for task of 9
- pool-1-thread-10 is looping of 10 for task of 10
- */
- //java新技术中的定时器的使用;相当于一次性炸弹
- Executors.newScheduledThreadPool(3).schedule(new Runnable() {
- @Override
- public void run() {
- System.out.println("bombing!!!");
- }
- },4,
- TimeUnit.SECONDS);
- //连环炸弹
- Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {
- @Override
- public void run() {
- System.out.println("bombing!!!");
- }
- },
- 4, //隔多少秒炸一下
- 2, //隔多少秒再炸
- TimeUnit.SECONDS);
- }
- }
线程高级应用-心得4-java5线程并发库介绍,及新技术案例分析的更多相关文章
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
1. HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...
- 线程高级应用-心得5-java5线程并发库中Lock和Condition实现线程同步通讯
1.Lock相关知识介绍 好比我同时种了几块地的麦子,然后就等待收割.收割时,则是哪块先熟了,先收割哪块. 下面举一个面试题的例子来引出Lock缓存读写锁的案例,一个load()和get()方法返回值 ...
- 线程高级应用-心得7-java5线程并发库中阻塞队列Condition的应用及案例分析
1.阻塞队列知识点 阻塞队列重要的有以下几个方法,具体用法可以参考帮助文档:区别说的很清楚,第一个种方法不阻塞直接抛异常:第二种方法是boolean型的,阻塞返回flase:第三种方法直接阻塞. 2. ...
- 线程高级应用-心得6-java5线程并发库中同步工具类(synchronizers),新知识大用途
1.新知识普及 2. Semaphore工具类的使用案例 package com.java5.thread.newSkill; import java.util.concurrent.Executor ...
- Java复习——多线程与并发库
开启一个线程 实现一个线程的方式有两种:继承Thread类.实现Runnable接口(也存在说三种的情况,第三种是使用线程并发库中的线程池创建一个线程).这两种方法都需要重写Run方法,具体的线程逻辑 ...
- Java多线程与并发库高级应用-java5线程并发库
java5 中的线程并发库 主要在java.util.concurrent包中 还有 java.util.concurrent.atomic子包和java.util.concurrent.lock子包 ...
- java--加强之 Java5的线程并发库
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9945499 01. 传统线程技术回顾 创建线程的两种传统方式: 1.在Thread子类覆盖的r ...
- java架构《并发线程高级篇一》
本章主要记录讲解并发线程的线程池.java.util.concurrent工具包里面的工具类. 一:Executor框架: Executors创建线程池的方法: newFixedThreadPool( ...
- Android多线程研究(7)——Java5中的线程并发库
从这一篇开始我们将看看Java 5之后给我们添加的新的对线程操作的API,首先看看api文档: java.util.concurrent包含许多线程安全.测试良好.高性能的并发构建块,我们先看看ato ...
随机推荐
- 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using S ...
- poj3342 Party at Hali-Bula
树形dp题,状态转移方程应该很好推,但一定要细心. http://poj.org/problem?id=3342 #include <cstdio> #include <cstrin ...
- ural 1113,jeep problem
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1113 网上的解答铺天盖地.我硬是花了两天才懂了点. wiki上的解释:https://e ...
- no branch 问题
现象如下: lynn.feng:~/project/Git/M_MT6737_MP$ git branch -a* (no branch) a36_panasonic_l004 b36_panason ...
- uml中定义的关系详细详解
uml定义的关系主要有六种:依赖.类属.关联.实现.聚合和组合.下面对其定义和表示方法逐一说明. 依赖(Dependency):元素A的变化会影响元素B,但反之不成立,那么B和A的关系是依赖关系,B依 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A
Description You are given names of two days of the week. Please, determine whether it is possible th ...
- js实现元素边框闪烁功能
<body> <input type="text" value="test" onclick="flash(this)"& ...
- MSComm函数说明(来自网络)
CommPort 设置并返回端口号 void CMSComm::SetCommPort(short nNewValue) short CMSComm::GetCommPort() RThreshold ...
- HDU 5615 Jam's math problem
Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...
- SqlSever基础 datepart 获取一个日期的年份
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...