版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26654727/article/details/78013989
最近在尝试重新复习一段关于多线程的使用,同时尝试使用关于markdown编辑器的使用方法,会同步将自己整理的文档放上来。
线程创建方式
通过创建一个线程类的方式创建线程体,例如实现runnable接口创建一个实现类。或者是直接通过创建Thread方式,重写内部的runnable方法实现线程体的编写。
1. 接用Thread.start 的方式进行重写runnable的方式进行实现线程。 继承Thread创建线程
new Thread(new Runnable() {
@Override
public 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{
@Override
public 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;
}
@Override
public 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);
}
@Override
public 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);
}
@Override
public 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--> 线程被中断,且当前线程已运行时间 --> 2000ms
at java.lang.Thread.sleep(Native Method)
at Thread.threadConcurrency01.Part02.Controller4Thread01.run(Controller4Thread01.java:22)
若有希望一起交流的朋友可以加我的有道云笔记的群,大家互相整理的技术栈。仅为交流 –>
(群号:51920822) –> http://163.fm/QO0DihJw