线程的常用操作
设置线程名字:setName()
获取线程名称:getName()
线程唯一Id:getId()
// 自定义线程名称 String threadName = "threadName"; // 构造方法方式 Thread thread = new Thread(() -> { System.out.println("线程名=" + Thread.currentThread().getName()); },threadName); // set方法方式 // thread.setName(threadName); System.out.println("线程唯一Id=" + thread.getId());
线程启动:start()
判断线程是否存活:isAlive()
// 线程启动 thread.start(); System.out.println("是否为存活线程=" + thread.isAlive());
线程方法:run() /call()
线程启动后会去调用的方法。线程要做什么就在run/call方法写,不需要直接调用,线程启动后自己会去调用run() /call()。如果程序没有启动线程直接调用run/call,那么就不属于多线程编程,是属于当前线程直接调用普通方法一样。
获取当前线程对象:currentThread()
操作当前线程的非static方法,得先拿到线程对象才可以
// 获取当前线程对象 Thread currentThread = Thread.currentThread(); // 对当前线程做一些操作 System.out.println(currentThread.getName()); try { // sleep 静态方法则不需要 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
关于线程的状态控制(生命周期)的操作可以参考上一篇文章。
守护线程(后台线程)
普通线程(用户线程)的守护者,守护线程的任务是为其他的线程提供服务。如果进程中没有了用户线程,那么守护线程也就没有存在的意义,JVM也随之结束。典型的守护线程有JVM的垃圾回收线程,操作系统的启动也会启动各种模块的守护线程。
设置线程为守护线程:setDaeman()
注意:该方法必须在start() 方法之前调用
public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("线程名="+Thread.currentThread().getName()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } // 这一句不会打印出来,因为main线程(目前唯一的普通线程)等待1秒后已经结束了 System.out.println("守护线程的状态=" + Thread.currentThread().getState()); }); // 守护线程 thread.setDaemon(true); // 线程启动 thread.start(); System.out.println("是否为守护线程=" + thread.isDaemon()); }
线程串行化
执行join() 方法的线程进入等待唤醒状态(WAITING),直到调用该方法的线程结束后再由等待唤醒状态转为可运行状态(RUNNABLE)。join() 方法是Thread类中的方法,其底层是使用wait() 方法来实现线程等待,待线程isAlive()为false 时才
实现线程的串行化:一个线程调用另一个线程对象的join() 来实现线程串行化执行。
举个例子:一道好菜
public class DemoCooking { public static void main(String[] args) { Thread mainThread = Thread.currentThread(); // 1.买菜 Thread buyThread = new Thread(new CookingThread(mainThread,"买菜"),"buyThread"); // 2.洗菜 Thread washThread = new Thread(new CookingThread(buyThread,"洗菜"),"washThread"); // 3.切菜 Thread cutThread = new Thread(new CookingThread(washThread,"切菜"),"cutThread"); // 4.炒菜 Thread scrambleThread = new Thread(new CookingThread(cutThread,"炒菜"),"scrambleThread"); // 不受线程启动顺序的影响 scrambleThread.start(); washThread.start(); cutThread.start(); buyThread.start(); // main线程先执行完才可以开始:买菜 System.out.println("开始准备……"); } public static class CookingThread implements Runnable{ private final Thread thread; private final String job; public CookingThread(Thread thread, String job){ this.thread = thread; this.job = job; } @Override public void run() { String name = Thread.currentThread().getName()+":"; try { thread.join(); System.out.println(name + job + "开始"); Thread.sleep(1000); System.out.println(name + job + "结束"); Thread.sleep(1000); // 偷懒下 } catch (InterruptedException e) { e.printStackTrace(); } } } }
执行结果:main > buyThread > washThread > cutThread > scrambleThread > 结束
开始准备……
buyThread:买菜开始
buyThread:买菜结束
washThread:洗菜开始
washThread:洗菜结束
cutThread:切菜开始
cutThread:切菜结束
scrambleThread:炒菜开始
scrambleThread:炒菜结束
线程优先级
设置当前线程的优先级,线程优先级越高,线程可能获得执行的次数越多,Java线程的优先级用整数表示,优先级的范围为1-10,默认为5。
setPriority(int)方法:设置线程的优先级。
getPriority方法:获取线程的优先级。
public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("线程1"); }); thread.setPriority(10); Thread thread1 = new Thread(() -> { System.out.println("线程2"); }); thread1.setPriority(1); thread.start(); thread1.start(); System.out.println("线程默认的优先级为=" + Thread.currentThread().getPriority()); }
线程中断
使用interrupt() 方法设置线程中断标志=true,让线程受到“阻塞”时抛出一个中断信号。如果线程处于阻塞、等待唤醒或超时等待状态(Object.wait, Thread.join和Thread.sleep)时,那么它将接收到一个中断异常(InterruptedException),从而提前被结束该状态。反之,如果线程是处于“可运行”(RUNNABLE)状态,那么中断标志将没有作用。
案例一:线程中断有效
public static void main(String[] args) { Thread thread = new Thread(() -> { System.out.println("线程1"); try { // 闹钟1分钟后响 Thread.sleep(60000); System.out.println("闹钟响了"); } catch (InterruptedException e) { // 提前退出超时等待状态 System.out.println("发生异常,提前醒了,闹钟没响手动关了"); } System.out.println("继续执行该线程的后续程序……"); }); thread.setPriority(1); thread.start(); thread.interrupt(); System.out.println("main线程将thread 终端状态设置为 "+thread.isInterrupted()); }
执行结果:
main线程将thread 终端状态设置为 true
线程1
发生异常,提前醒了,闹钟没响手动关了
继续执行该线程的后续程序……
案例二:线程中断无效
public static void main(String[] args) { Thread thread1 = new Thread(() -> { System.out.println("线程" + Thread.currentThread().getName()); while (true) { System.out.print(Thread.currentThread().getState() + "\t"); } }); thread1.start(); thread1.interrupt(); }
执行结果:线程一直打印自己的状态为RUNNABLE。
以上是Java线程常用的操作有哪些?的详细内容。更多信息请关注PHP中文网其他相关文章!

Docker Nginx部署前端项目遇到的空白页及代理异常问题在使用Docker和Nginx部署前后端分离项目时,经常会遇到一些�...

SpringBoot3项目外部配置文件的部署方法在SpringBoot3项目开发中,我们经常需要将配置文件application.properties...

将Apache的.htaccess配置转换为Nginx的配置方法在项目开发中,经常会遇到需要将服务器从Apache迁移到Nginx的情况。Ap...

JavaWeb应用性能优化:Dao层实体类缓存的可行性探讨在JavaWeb应用开发中,性能优化一直是开发者关注的重点。尤�...

在高并发环境下如何保证脚本任务的唯一性和监控其运行状态?本文将探讨如何在集群环境中,确保一个出库脚...

关于子类如何通过继承父类的setName方法设置私有属性在编程中,特别是在面向对象编程的语言如Java中,子类与�...

如何解决使用EclipsePaho的MqttAsyncClient连接本地EMQX时用户名密码认证失败的问题?在使用Java和Eclipse...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具