目录
一、前言
二、线程和进程
三、多线程的创建
多线程创建的两种方式:
方式①:
方式②:
四、几个常见的问题?
为什么要重写run方法?
run()方法和start方法的区别?
通过继承的方法和实现接口的方式创建多线程,哪个好?
五、线程控制
sleep:
join
setDaemon():
六、线程的生命周期 一、前言
线程可能听起来有点难,其实也是非常好理解的,咱们以游戏姿势进入。 二、线程和进程
进程:是正在运行的,进程是资源分配的最小单位。
线程:是cpu调度的最小单位(线程依赖于进程)。
上面可能有些难懂,打个比喻,好比你打一把王者(其实我不玩哈doge),进程比作是你
开的那一把游戏,线程比作成每个玩家所选的英雄或者是游戏中的水晶野怪等之类的。带着
这个比喻来理解进程和线程的一些关系。
一个进程有多个线程就叫多线程。是不是感觉非常好理解。
1.线程在进程下进行
1.(比如你单独的英雄角色、野怪、小兵肯定不能运行)
2.进程之间不会相互影响,一个线程结束将会导致整个进程结束
2.(两把游戏之间不会有联系和影响。你的水晶被推掉,你这把游戏就结束了)
3.不同的进程数据很难共享
3.(两把游戏之间很难有联系,有联系的情况比如上把的敌人这把又匹配到了)
4.同进程下的不同线程之间数据很容易共享
4.(你开的那一把游戏,你可以看到每个玩家的状态(生死),也可以看到每个玩家的出装备等等)
5.进程的使用内存地址可以限定使用量
5.(开的房间模式,你可以设置有多少人进,当房间满了后,其他人就进不去了,除非有人退出房间,其他人才能进)
推荐阅读:《Java高并发编程详解-多线程与架构设计》,点此可免费领取电子版书籍
三、多线程的创建
多线程创建的两种方式:
♠①:创建一个类继承Thread类,并重写run方法。
♠②:创建一个类实现Runnable接口,并重写run方法。
方式①:
MyThread类下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class MyThread extends Thread {
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(getName()+":打了"+i+"个小兵");
}
}
}</pre>
MyThreadText类下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class MyThreadText {
public static void main(String[] args) {
//创建MyThread对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
//设置线程的名字
t1.setName("鲁班");
t2.setName("刘备");
t3.setName("亚瑟");
//启动线程
t1.start();
t2.start();
t3.start();
}
}</pre>
效果图:
方式②:
MyRunnable类下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class MyRunnable implements Runnable {
@Override
public void run() {
for(int i=0;i<10;i++){
try {//sleep会发生异常要显示处理
Thread.sleep(20);//暂停20毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"打了:"+i+"个小兵");
}
}
}</pre>
MyRunnableText类下:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n51" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class MyRunnableText {
public static void main(String[] args) {
//创建MyRunnable类
MyRunnable mr=new MyRunnable();
//创建Thread类的有参构造,并设置线程名
Thread t1=new Thread(mr,"张飞");
Thread t2=new Thread(mr,"貂蝉");
Thread t3=new Thread(mr,"吕布");
//启动线程
t1.start();
t2.start();
t3.start();
}
}</pre>
有sleep暂停的效果图:
推荐阅读:《Java高并发编程详解-多线程与架构设计》,点此可免费领取电子版书籍
四、几个常见的问题? 为什么要重写run方法?
因为run方法是用来封装被线程执行的代码。
un()方法和start方法的区别?
run():封装线程执行的代码,直接调用相当于调用普通方法。就是一直到这个方法结束才出来。 start():启动线程,然后由JVM此线程的run()方法,可同时进行多个对象调用start()方法。
通过继承的方法和实现接口的方式创建多线程,哪个好?
实现Runable接口好,原因:
①避免了Java单继承的局限性
②适合多个相同的程序代码去处理同一资源的情况,把线程、代码和数据有效的分离,
体现出来面向对象的设计思想。
五、线程控制
sleep:
sleep的使用要进行显示处理异常:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n112" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> try {//sleep会发生异常要显示处理
Thread.sleep(20);//暂停20毫秒
} catch (InterruptedException e) {
e.printStackTrace();
}</pre>
join:
♠ join是指等待这个线程执行完才会轮到后续线程得到cpu的执行权,使用这个也要抛出异常
具体使用:
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n118" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class MyThreadText {
public static void main(String[] args) throws InterruptedException {//throws抛出join出现的异常
//创建MyThread对象
MyThread t1=new MyThread();
MyThread t2=new MyThread();
MyThread t3=new MyThread();
//设置线程的名字
t1.setName("鲁班");
t2.setName("刘备");
t3.setName("亚瑟");
//启动线程
t1.start();
t1.join();//等待t1执行完才会轮到t2,t3抢
t2.start();
t3.start();
}
}</pre>
setDaemon():
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="" cid="n120" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"> t1.setName("张飞");
t2.setName("关羽");
t3.setName("刘备");
t1.setDaemon(true);
t2.setDaemon(true);</pre>
为true时说明是守护线程。
将t1,t2设置守护线程t3就是主线程了, 则当所有的主线程结束后,守护线程也会跟着
结束,但不是立刻结束。
六、线程的生命周期