java并发编程实践 part 01 --gt; 线程创建方式
最近在尝试重新复习一段关于多线程的使用,同时尝试使用关于markdown编辑器的使用方法,会同步将自己整理的文档放上来。
线程创建方式
通过创建一个线程类的方式创建线程体,例如实现runnable接口创建一个实现类。或者是直接通过创建Thread方式,重写内部的runnable方法实现线程体的编写。
1. 接用Thread.start 的方式进行重写runnable的方式进行实现线程。 继承Thread创建线程
new Thread(new Runnable() {@Overridepublic void run() {while(true){System.out.println("just a test " + Thread.currentThread().getName()+ " "+new Date());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();输出结果:
just a test Thread-0 Sun Sep 17 09:48:48 CST 2017
just a test Thread-0 Sun Sep 17 09:48:49 CST 2017
just a test Thread-0 Sun Sep 17 09:48:50 CST 2017
just a test Thread-0 Sun Sep 17 09:48:51 CST 2017
just a test Thread-0 Sun Sep 17 09:48:52 CST 2017
just a test Thread-0 Sun Sep 17 09:48:53 CST 2017
2.创建一个实现Runnable接口并重写run方式的实现类,来进行线程体逻辑的可重用。
public class CreatThread02 implements Runnable{@Overridepublic void run() {System.out.println("currentThread : "+ Thread.currentThread().getName()+ " say hello for you ... time --> " + new Date());}public static void main(String[] args) {Runnable thread01 = new CreatThread02();Runnable thread02 = new CreatThread02();while (true){try {thread01.run();Thread.sleep(500);thread02.run();Thread.sleep(400);} catch (InterruptedException e) {e.printStackTrace();}}}
}输出结果:
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:45 CST 2017
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:45 CST 2017
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:46 CST 2017
currentThread : main say hello for you ... time --> Sun Sep 17 10:02:47 CST 2017
3.通过创建线程池的方式创建线程
public static void main(String[] args) {ExecutorService service = Executors.newFixedThreadPool(2);for(int i = 0 ; i< 100 ;i++){RunClass run = new RunClass(i);service.execute(run);}service.shutdown();}static class RunClass implements Runnable{private int index ;public RunClass(int index ){this.index = index;}@Overridepublic void run() {long sleeptime = (long)(Math.random()*1000);System.out.println("the RunClassNum--> "+index +" cunrrentThread --> "+ Thread.currentThread().getName()+ " come back over ..." + " Sleep Time -->" +sleeptime);try {Thread.sleep(sleeptime);} catch (InterruptedException e) {e.printStackTrace();}}}输出结果:
the RunClassNum--> 87 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->152
the RunClassNum--> 88 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->207
the RunClassNum--> 89 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->958
the RunClassNum--> 90 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->484
the RunClassNum--> 91 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->767
the RunClassNum--> 92 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->202
the RunClassNum--> 93 cunrrentThread --> pool-1-thread-2 come back over ... Sleep Time -->666
the RunClassNum--> 94 cunrrentThread --> pool-1-thread-1 come back over ... Sleep Time -->454
Executors类中存在多个线程池类型,具体分为以下几种:
- public static ExecutorService newFixedThreadPool(int nThreads) :
可创建指定数量线程的线程池,当有新的线程需要执行,同时线程池内有空余线程,则会直接取用当前空闲线程,来达到线程的利用率。 - public static ThreadFactory defaultThreadFactory();
返回线程池的默认线程创建工厂 - public static ExecutorService newCachedThreadPool();
创建一个线程池,根据需要适当的创建线程的方式,
4.线程的控制 sleep、join、interrupt
- sleep–>使当前线程暂停一段时间
- join–>使当前的线程加入另一个线程
public class Controller4Thread01 extends Thread {// 1.使用sleep使当前线程暂停一段时间
// 2.使用join是当前线程加入另一个线程public static int result;public Controller4Thread01(String name ){super(name);}@Overridepublic void run() {result = (int)( Math.random()*1000);System.out.println("currentThread name-->"+ this.getName() + " get result -->" + result);try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}}public static void main(String[] args) {Controller4Thread01 thread01 = new Controller4Thread01("测试线程1");thread01.start();long startTime = System.currentTimeMillis();System.out.println("开始时间为--> " + startTime);System.out.println("thread 未加入之前 .. result-->"+result);try {thread01.join();} catch (InterruptedException e) {e.printStackTrace();}long endTime = System.currentTimeMillis();System.out.println("结束时间为--> "+ endTime + " 时间间隔-->" + (endTime-startTime));}
}输出结果:
- interrupt–>打断当前的线程
public class Controller4Thread01 extends Thread {// 1.使用sleep使当前线程暂停一段时间
// 2.使用join是当前线程加入另一个线程
// 3.使用interrupt打断线程public static int result;public long time ;public Controller4Thread01(String name ){super(name);}@Overridepublic void run() {long starttime = System.currentTimeMillis();try {result = (int)( Math.random()*1000);System.out.println("currentThread name-->"+ this.getName() + " get result -->" + result);Thread.sleep(4000);long endTime = System.currentTimeMillis();time = endTime-starttime;System.out.println(this.getName() + "--> 线程正常运行,且当前线程已运行时间 --> "+ time + "ms");} catch (InterruptedException e) {long endTime = System.currentTimeMillis();time = endTime-starttime;System.out.println(this.getName() + "--> 线程被中断,且当前线程已运行时间 --> "+ time + "ms");e.printStackTrace();}}public static void main(String[] args) {Controller4Thread01 thread01 = new Controller4Thread01("测试线程1");thread01.start();long startTime = System.currentTimeMillis();System.out.println("开始时间为--> " + startTime);System.out.println("thread 未加入之前 .. result-->"+result);try {thread01.join(2000);long endTime = System.currentTimeMillis();thread01.interrupt();System.out.println("结束时间为--> "+ endTime + " 时间间隔-->" + (endTime-startTime));} catch (InterruptedException e) {e.printStackTrace();}}
}
//输出结果:
开始时间为--> 1505642692997
thread 未加入之前 .. result-->0
currentThread name-->测试线程1 get result -->648
结束时间为--> 1505642694998 时间间隔-->2001
java.lang.InterruptedException: sleep interrupted
测试线程1--> 线程被中断,且当前线程已运行时间 --> 2000msat java.lang.Thread.sleep(Native Method)at Thread.threadConcurrency01.Part02.Controller4Thread01.run(Controller4Thread01.java:22)
若有希望一起交流的朋友可以加我的有道云笔记的群,大家互相整理的技术栈。仅为交流 –>
(群号:51920822) –> http://163.fm/QO0DihJw
java并发编程实践 part 01 --gt; 线程创建方式相关推荐
- JAVA并发编程实践笔记
2019独角兽企业重金招聘Python工程师标准>>> JAVA并发编程实践笔记 博客分类: java JAVA并发编程实践笔记 1, 保证线程安全的三种方法: a, 不要跨 ...
- java并发编程实践(2)线程安全性
[0]README 0.0)本文部分文字描述转自:"java并发编程实战", 旨在学习"java并发编程实践(2)线程安全性" 的相关知识: 0.1)几个术语( ...
- 《Java并发编程实践》学习笔记之一:基础知识
<Java并发编程实践>学习笔记之一:基础知识 1.程序与进程 1.1 程序与进程的概念 (1)程序:一组有序的静态指令,是一种静态概念: (2)进程:是一种活动,它是由一个动作序列组成 ...
- Java并发编程实战_一线大厂架构师整理:java并发编程实践教程
并发编程是Java语言的重要特性之一, 在Java平台上提供了许多基本的并发功能来辅助开发多线程应用程序.然而,这些相对底层的并发功能与上层应用程序的并发语义之间并不存在一种简单而直观的映射关系.因此 ...
- java并发编程实践_Java并发编程实践如何正确使用Unsafe
一.前言 Java 并发编程实践中的话: 编写正确的程序并不容易,而编写正常的并发程序就更难了.相比于顺序执行的情况,多线程的线程安全问题是微妙而且出乎意料的,因为在没有进行适当同步的情况下多线程中各 ...
- java并发编程实践(1)intro
[0]README 0.1)本文部分文字描述转自"java并发编程实践",旨在学习"java并发编程实践(1)intro"的相关知识: [3]线程带来的风险 [ ...
- [Java 并发] Java并发编程实践 思维导图 - 第一章 简单介绍
阅读<Java并发编程实践>一书后整理的思维导图.
- 【Java并发编程】之二:线程中断
[Java并发编程]之二:线程中断 使用interrupt()中断线程 当一个线程运行时,另一个线程可以调用对应的Thread对象的interrupt()方法来中断它,该方法只是在目标线程中设置一 ...
- java并发编程实践-带完整书签pdf电子扫描版
2007年6月由电子工业出版社出版发行,是一本经典的Java并发参考手册.java并发编程实践随着多核处理器的普及,使用并发成为构建高性能应用程序的关键.Java5以及6在开发并发程序中取得了显著的进 ...
最新文章
- Java基础学习总结(14)——Java对象的序列化和反序列化
- 《周四橄榄球之夜》流媒体视频拆解:Twitch VS Amazon Prime
- JavaScript——判断undefined解决方案
- Python爬虫基本库的使用
- C语言计算分段函数pta,PTA浙大版《C语言程序设计(第3版)》题目集 练习2-11 计算分段函数[2] (10分)...
- BGP——路由通告+IBGP水平分割机制+RR路由反射器(讲解+配置命令)
- 用css实现星级评分效果
- Intel Haswell/Broadwell架构/微架构/流水线 (6)-Unlamination微指令离解过程
- 在Firefox中通过AJAX跨域访问Web资源
- jQuery操作DOM节点的相关方法
- oracle fileperset,RMAN具体使用方法
- 简单理解javascript中的原型对象,实现对之间共享属性和行为
- C语言课设图书管理系统(大作业)
- ffmpeg添加自定义硬件编解码器
- ibm z系列服务器图片,IBM Z系列本本最清晰图片全面曝光(图)
- 泰凌微 Telink TLSR825X Uart 串口无法接收数据 关闭休眠和深度休眠解决 问题
- 服务器复制文件出现io错误,win7系统复制文件时发生IO错误的解决方法
- 云展网教程 | 云展网电子杂志页面排版最佳尺寸,最佳字体,字号
- buck电路上下管_分时供电全桥Buck型双输入直流变换器
- 怎么确定电视吊架安装位置,电视支架安装讲解