栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java—线程的生命周期及线程控制方法详解,java常用的设计模式面试

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java—线程的生命周期及线程控制方法详解,java常用的设计模式面试

*/

//若线程不是就绪状态,就抛出异常

if (threadStatus != 0)

throw new IllegalThreadStateException();

//将线程添加到ThreadGroup中

group.add(this);

boolean started = false;

try {

//通过start0()方法启动线程

start0();

//设置线程启动的started标志位

started = true;

} finally {

try {

if (!started) {

group.threadStartFailed(this);

}

} catch (Throwable ignore) {

}

}

}

start()实际上通过本地方法start0()启动线程,会新运行一个线程,新线程会调用run()方法。

run()方法源码

@Override

public void run() {

if (target != null) {

target.run();

}

}

target是Runnable对象,run()直接调用Thread线程的Runnable成员的run()方法,并不会新建一个线程。

线程控制方法

sleep()方法

sleep()方法介绍

sleep(long millis)方法是Thread类的一个静态方法,作用是让当前线程暂停一段时间,并进入阻塞状态。

sleep()方法重载方式

public static native void sleep(long millis) throws InterruptedException:让当前正在执行的线程暂停millis毫秒,并进入阻塞状态。

public static void sleep(long millis, int nanos) throws InterruptedException:让当前正在执行的线程暂停millis毫秒+nanos毫微秒,并进入阻塞状态。(很少用)

sleep()示例

通常用法就是

//让当前线程睡眠1000毫秒,即暂定1s

Thread.sleep(1000);

yield()方法

yield()方法介绍

yield()方法让当前正在执行的线程暂停,但不会阻塞线程,只是让线程转入就绪状态。

yield()方法让当前线程暂停,让系统的线程调度重新调度一次,所以会出现当某个线程调用了yield()方法后,线程调度器又重新将它调度出来执行。

yield()方法让当前线程暂停后,只有优先级>=当前线程的处于就绪状态的线程才能获取CPU执行权限。

yield()方法重载

public static native void yield();:静态方法。

yield()示例

//让当前线程暂停

Thread.yield();

线程优先级

每个线程执行都有一定的优先级,优先级高的线程获得CPU执行权限的机会比较大。

每个线程默认的优先级与创建它的父线程的优先级相同。所以main线程的优先级一般和自己创建的子线程优先级一样。

Thread类提供setPriority(int newPriority)和getPriority()方法设置和返回指定线程的优先级。其中setPriority()方法的参数可以是一个整数(1-10之间),也可以是静态常量。

MAX_PRIORITY:值为10.

MIN_PRIORITY:值为1.

NORM_PRIORITY:值为5.

join()方法

join()方法介绍

Thread类提供join()方法让一个线程等待另一个线程完成的方法;就是将指定的线程加入到当前线程,这样两个交替执行的线程就变成了顺序执行的线程,如线程A调用了线程B的join()方法,则线程A会等待线程B执行完毕后才会继续执行自己。

join()方法由使用线程的程序调用,调用线程调用线程t的t.join()方法后将会被阻塞,直到线程t执行完毕,调用线程才能继续执行。一般就是用于主线程内,等待其他线程执行完毕后,再继续执行main()函数。

join()方法重载方式

public final void join() throws InterruptedException:等待被join的线程执行完毕。

public final synchronized void join(long millis) throws InterruptedException:等待被join的线程的超时时间为millis毫秒。如果在millis毫秒内被join的线程还未结束执行流程,则调用线程不再等待。

public final synchronized void join(long millis, int nanos) throws InterruptedException:等待被join的线程的时间最长为millis毫秒+nanos毫微秒。(很少用)

join()方法示例

(1)未使用join()方法

代码

public class JoinMethodTest {

public static void main(String[] args) {

System.out.println(“main thread start”)

【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】

浏览器打开:qq.cn.hn/FTf 免费领取

;

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

System.out.println(“child thread start”);

try {

Thread.sleep(1000);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(“child thread finshed”);

}

});

thread.start();

System.out.println(“main thread finshed”);

}

}

运行结果

main thread start

main thread finshed

child thread start

child thread finshed

可以从运行结果看出,main()主线程日志打印的很快,没有等待子线程打印就结束了。

(2)使用join()方法

代码

public class JoinMethodTest {

public static void main(String[] args) {

System.out.println(“main thread start”);

Thread thread = new Thread(new Runnable() {

@Override

public void run() {

System.out.println(“child thread start”);

try {

Thread.sleep(1000);

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(“child thread finshed”);

}

});

thread.start();

//加入join()方法等待子线程执行完毕,才执行主线程。

try {

thread.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(“main thread finshed”);

}

}

运行结果

main thread start

child thread start

child thread finshed

main thread finshed

从运行结果可以看出,main thread finshed结果是在最后打印的,加入join()方法等待子线程执行完毕,才执行主线程。

6种状态的线程生命周期解释

Q&A

为何启动线程需要用start()方法而不是直接调用run()方法?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/489779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号