当前位置: 首页 > news >正文

Thread类的基本用法

线程创建

  1. 继承 Thread, 重写 run

package thread;class myThread extends Thread{@Overridepublic void run(){while (true) {System.out.println("hello thread");try {Thread.sleep(1000);}catch (InterruptedException e) {e.printStackTrace();}}}
}
public class Demo1 {public static void main(String[] args) {System.out.println("你好");myThread thread=new myThread();// thread.run();thread.start();//开启线程,执行run方法,输出}
}

 

2.实现 Runnable, 重写 run

package thread;
class myRunnable implements Runnable {@Overridepublic void run(){while (true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}
}public class Demo2 {public static void main(String[] args) throws InterruptedException {//Runnable是接口 myRunnable是自定义的类 注意写法Runnable runnable=new myRunnable();Thread thread=new Thread(runnable);thread.start();while (true){System.out.println("hello main");Thread.sleep(1000);}}
}

 

3.继承 Thread, 重写 run, 使用匿名内部类

package thread;
//继承 Thread, 重写 run, 使用匿名内部类
public class Demo3 {public static void main(String[] args) throws InterruptedException {Thread t =new Thread(){@Overridepublic void run(){while (true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};t.start();while (true){System.out.println("hello main");Thread.sleep(1000);}}
}

 

4.实现 Runnable, 重写 run, 使用匿名内部类

package thread;public class Demo4 {public static void main(String[] args) throws InterruptedException {Runnable runnable=new Runnable() {@Overridepublic void run(){while (true){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};Thread thread=new Thread(runnable);thread.start();while (true){System.out.println("hello main");Thread.sleep(1000);}}
}

 

5.使用 lambda 表达式 

    package thread;public class Demo5 {public static void main(String[] args) {// 通过 lambda 表达式, 来创建线程Thread t = new Thread(() -> {while (true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});t.start();while (true) {System.out.println("hello main");try {Thread.sleep(1000); } catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
    }

    线程中断

    中断的方法:

    ·手动创建标志位

    ·线程内置的标志位isinterruptted

            --true就是有人尝试终止线程等待线程休眠获取线程实例

    ·将标志位从false改成true

    Thread thread = new Thread(() -> {while(!Thread.currentThread().isInterrupted()) {// 执行任务}
    });
    thread.start();// 中断线程
    thread.interrupt();

    这里要注意一个点:

    ·不能直接引用:

            --t这里可能还没初始化完成-->右侧的new Thread(...)lambda表达式要先完成构造,但这里构造器本身包含了t。

    正确做法:在runnable中引用当前线程

    ·哪个线程调用这个方法就返回哪个线程对象的引用

    ·在t start了之后才会执行

    结合睡眠的一个点:

    ·可能还没睡眠完就终止了。---如果t线程在sleep,然后main调用interrupt方法---提前唤醒

    ·catch里面的异常支持sleep提前唤醒---提前唤醒>触发异常>清空标志位(重置为false)抛出interruptedException>while执行

    解决:

    ·加一个break,结束循环,让线程结束

    ·在catch里面的break之前可以加一些其他逻辑

    ·注意:如果直接throw new RuntimeException

            不只是让t线程结束,而且会直接导致整个线程结束,没有人catch这个Runtime异常

    package thread;public class Test4 {public static void main(String[] args) throws InterruptedException {Thread t=new Thread(()->{//while(!t.isInterrupted()){while (!Thread.currentThread().isInterrupted()){System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {//e.printStackTrace();break;}}});t.start();Thread.sleep(2000);t.interrupt();}

    ·interrupt被调用的时候t在休眠的话sleep()会立即抛出InterruptedException,执行catch中的break 

    线程等待

    ·join

    ·约定了结束的先后顺序

    ·在哪个线程调用join那这个线程就是等的一方,join前面是哪个引用那就是被等的一方

    ·join保证了被等的一方线程执行完毕

    ·如果被等的一方是死循环,那就一直阻塞状态

    ·join的等待是“死等”

    ·通常加一个最大等待时间:

    public void join(long millis)//等待线程结束,最多等millis毫秒

    线程休眠

    用sleep控制线程休眠

    让线程变成阻塞状态,不参与cpu调度--->时间到了之后就回到就绪状态(不是立即执行)

    获取线程实例

    获取当前线程:currentThread

    获取jvm中所有线程:getAllStackTraces()

    获取所有活动进程:getAllStackTraces().keySet()

    获取线程组中的线程:currentThread().getThreadGroup()

    http://www.xdnf.cn/news/645769.html

    相关文章:

  1. DOM事件的传播机制
  2. 贪心算法应用:最大匹配问题详解
  3. Ollama学习1:安装、命令、API
  4. C++语言入门————高精度计算
  5. 基于RK3568处理器实现8路CAN总线PLC解决方案
  6. numpy执行无缘无故崩溃 没有报错
  7. Autodl训练Faster-RCNN网络--自己的数据集(二)
  8. PCB文件从 Allegro 24.1 降级保存为 Allegro 17.4版本格式
  9. 李沐《动手学深度学习》| 4.4 模型的选择、过拟合和欠拟合
  10. Mujoco 学习系列(六)官方教程 The introductory tutorial teaches MuJoCo basics
  11. 53页 @《人工智能生命体 新启点》中國龍 原创连载
  12. Learning Transferable Visual Models From Natural Language Supervision
  13. 国内云平台RTX 5090租赁及LLM微调推荐
  14. 系统编程day04
  15. 分库分表深度解析
  16. Go语言Map的底层原理
  17. springboot 控制层调用业务逻辑层,注入报错,无法自动装配 解决办法
  18. [yolov11改进系列]基于yolov11的骨干轻量化更换backbone为shufflenetv2网络python源码+训练源码
  19. Win11亮度条和亮度设置消失的解决方法
  20. Go并发模式详解:Fan-in与Fan-out的实战应用
  21. lec11-并发控制
  22. LeetCode --- 450周赛
  23. 自动化测试②
  24. loss的范围
  25. 创建一个PCB封装(Altium Designer)
  26. pandas高效实现数据对比解决方案
  27. DBLab:一个免费的AI数据库管理工具
  28. QML学习07Property
  29. 使用Zotero的RSS订阅功能快速了解感兴趣领域最新文章
  30. LLM基础-什么是大模型推理(LLM Inferencing)