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

Thread类的基本用法(上)

一、线程创建方法(5种)

1.继承Thread

class MyThread extends Thread {@Overridepublic void run() {System.out.println("MyThread is running");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}

这里中间运行的代码,需要抛出异常。

然后创建MyThread的实例,并执行

public class Demo1 {public static void main(String[] args) throws InterruptedException {Thread t = new MyThread();t.start();//t.run();while(true){System.out.println("hello world");Thread.sleep(1000);}}
}

2.实现Runnable接口

class MyRunnable implements Runnable {@Overridepublic void run() {while (true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}
}

然后创建Thread实例,将Runnable对象作为参数。

public class Demon2 {public static void main(String[] args) throws InterruptedException {Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}

这里也可以写成

public class Demon2 {public static void main(String[] args) throws InterruptedException {
/*        Runnable runnable = new MyRunnable();Thread thread = new Thread(runnable);*/Thread thread = new Thread(new MyRunnable());thread.start();while(true) {System.out.println("hello main");Thread.sleep(1000);}}
}

后面的使用方法不变。

3.使用匿名内部类创建Thread子类对象

public class Demon2 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(){@Overridepublic void run() {while(true) {System.out.println("hello world");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}}};t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}

实际上就是第一种方法的变形。

4.匿名内部类创建Runnable子类对象

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

后面依旧需要创建Thread实例化,将Runnable作为对象参数传入。

5.使用lambda表达式创建子类

public class Demon4 {public static void main(String[] args) throws InterruptedException {Thread t  = new Thread(()->{while(true) {System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}});t.start();while(true) {System.out.println("hello my");Thread.sleep(1000);}}
}

这也是最简单最常用的方法。

二、线程中断

这里采用Thread中的interrupted方法

public class Demon10 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(()->{while(!Thread.currentThread().isInterrupted()){//System.out.println("t:"+Thread.currentThread().getName());System.out.println("hello thread");try {Thread.sleep(1000);} catch (InterruptedException e) {//throw new RuntimeException(e);break;}}System.out.println("t结束");});t.start();//System.out.println("main:"+Thread.currentThread().getName());Thread.sleep(3000);System.out.println("main尝试终止");t.interrupt();}
}

t.interrupt在调用后内部类中的while循环条件被打破,直接打印“t结束”。


文章转载自:

http://aC764Iqb.gftnx.cn
http://W07PKKEL.gftnx.cn
http://NKIKppb5.gftnx.cn
http://BAYYnNWQ.gftnx.cn
http://OOrvAdQr.gftnx.cn
http://FLUAc6r9.gftnx.cn
http://S2OJRt0i.gftnx.cn
http://7aQ5Aa9R.gftnx.cn
http://NNI6Wo8V.gftnx.cn
http://ax6i03V2.gftnx.cn
http://8qYcxfAS.gftnx.cn
http://XGrdaJcT.gftnx.cn
http://f3xQr2D5.gftnx.cn
http://lLMpAw6y.gftnx.cn
http://U9sQZDte.gftnx.cn
http://iWy0N3kt.gftnx.cn
http://XGkSZbQi.gftnx.cn
http://Dz9GvZyb.gftnx.cn
http://UeY56tUG.gftnx.cn
http://LVjcfVa8.gftnx.cn
http://gF3lu1uU.gftnx.cn
http://VgOphtB8.gftnx.cn
http://tD8KdBCU.gftnx.cn
http://92Fkl1xx.gftnx.cn
http://3vw6U9iT.gftnx.cn
http://box7yYo2.gftnx.cn
http://uWtiICnA.gftnx.cn
http://Qa3jJhwe.gftnx.cn
http://yZiYoF9Z.gftnx.cn
http://JZTDxgeM.gftnx.cn
http://www.dtcms.com/a/377727.html

相关文章:

  • 数据建模的真相!为什么90%的团队都在做无用功
  • 30 分钟让 AI 开口查订单:React-Native + Coze 全链路语音对话落地指南
  • Nacos报错NacosException: Client not connected, current status:STARTING
  • 基于SpringBoot+Vue2开发的母婴育婴师平台
  • GNU 工具链与ARM 交叉编译工具链
  • 【大模型应用开发 6.LlamaIndex-Workflow】
  • 【蓝桥杯 2024 国 Java A】粉刷匠小蓝
  • Android 编译系统lunch配置总结
  • 2024-2025-2Linux课堂笔记及作业(不完整版)
  • ELF文件的组成格式的详细介绍
  • vue中通过heatmap.js实现热力图(多个热力点)热区展示(带鼠标移入弹窗)
  • Java基础 9.10
  • 绿色算力技术栈:AI集群功耗建模与动态调频系统
  • 从零搭建网站(第五天)
  • MySQL 8.4.6 安装
  • 前端架构知识体系:Web Worker 使用与优化指南
  • 嵌入式 - ARM4
  • Linux 的权限详解
  • 研究生开题答辩全攻略!老学姐教你轻松过关,再也不用担心被老师刁难!
  • Angr符号执行初步学习
  • Shell编程之正则表达式与文本处理工具
  • 软考系统架构设计师之UML统一建模语言
  • malloc概述
  • Nginx 实战系列(九)—— LVS负载均衡集群与DR模式部署指南
  • 利用美团龙猫用libxml2编写XML转CSV文件C程序
  • NJet支持使用json格式的配置文件了
  • 平时只会CRUD,没有高质量项目经验,我该怎么办
  • Vue项目创建方式(多种)
  • PMM:一款开源的数据库监控与管理工具
  • pyinstaller打包多个文件成一个exe