当前位置:网站首页>多线程的具体使用
多线程的具体使用
2022-07-26 23:17:00 【月半的人】
首先我们先开看看多线程的具体创建:
首先实现多线程的创建有多种方法->
1.继承Thread,重写
class MyThread extends Thread{
@Override
public void run() {
}
}
public class TextDemo {
public static void main(String[] args) {
Thread thread = new MyThread();
}
}
2.实现Runnable
class MyRunning implements Runnable{
@Override
public void run() {
}
}
public class TextDemo {
public static void main(String[] args) {
MyRunning running = new MyRunning();
Thread thread = new Thread(running);
}
}3.继承Thread,重写run,使用匿名内部类
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
super.run();
}
};
}
}4.实现Runable,重写run,使用内部表达式
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
}
});
}
}使用lambda表达式
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(()->{
System.out.println("hello thread");
});
}
}以上就是基本的线程创建的方法.
Thread 的创建构造方法其实上不止还有添加Runnable
实际上还能给Thread给添加名字,为了让程序猿方便调试线程
| 方法 | 解释 |
| Thread(String name) | 创建线程对象并且命名 |
| Thread(Running target,String name) | 使用Running创建对象,并命名 |
Thread 的常见属性

ID是线程的位移标识,不同县城不会重复
名称是各种调试工具可以用到
状态表示现成当前处于一个情况
优先级高的现成理论上来说更容易调度
关于后台线程,需要记住一点:JVM会在一个进程的所有后台线程结束后,才会结束运行.
是否存活,即使简单的理解,为run方法是否运行结束
| 属性 | 获取方法 |
| ID | getid() |
| 名称 | getNanme() |
| 状态 | getState() |
| 优先级 | getPriority() |
| 是否后台线程 | isDaemon() |
| 是否存活 | isAlive() |
| 是否中断 | isInterrupted() |
中断线程的方法
1.使用自定义的变量作为标志位.
2.需要给标志位商家volatile
private static boolean isQuit = false;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(()->{
while(!isQuit){
System.out.println("Keep running");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
thread.sleep(5000);
System.out.println("stop running");
isQuit = true;
System.out.println("already stop");
}自设定标志位.
实例->使用Thread.interrupted() 或者Thre.currentThread().isInterrupted() 代替自定义标志位
Thread内部包含一个Boolean类型的变量作为线程是否被中断的标记
首先有三种方法:
| 方法 | 说明 |
| public void interrupt() | 中断对象关联的线程,如果线程正在阻塞,则以异常的方式通知 |
| public static boolean interrupted() | 判断当前线程的中断标志位是否设置,调用后清楚标志位.线程的“中断状态”由该方法清除。换句话说,如果连续两次调用该方法,则第二次调用将返回 false。 |
| public boolean isinterrupted() | 判断对相关联的线程的标志位是否设置,调用后不用清楚标志位 |
thread收到通知的方式有两种:
1.如果线程因为调用wait/join/sleep 等方法而阻塞挂起,则以InterruptedException异常的形式通知,清楚中断标志
当出现InterruptedException 的时候,要不要借宿线程取决于Catch中代码的写法.可以选择忽略这个异常,也可以跳出循环结束线程.
2.否则,姿势内部的一个中断标志被蛇子,thread可以通过
Thread.interrupted() 判断当前线程的中断标志被设置 ,清楚中断标志.
Thread.currentThread().isinterrupted()判断指定线程的中断标志被设置,不清除中断标志这种方式通知手到的更加及时,即是县城正在sleep也可以马上手到.
边栏推荐
猜你喜欢

Prompt to leave the page

Hcip day 1

BigDecimal 的 4 个坑,你踩过几个?

C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
![[use SQLite3 library to realize student information management system in Visual Studio 2019]](/img/49/5d6c47654e446ab96099a821a16369.png)
[use SQLite3 library to realize student information management system in Visual Studio 2019]

Is it useful to lie down with your eyes closed when you can't sleep?

Wechat applet: user wechat login process (attached: flow chart + source code)

【斐波那契数列及螺线 基于C语言】

Hcip day 6 OSPF static experiment

HCIP oSPF综合实验
随机推荐
PPOCRLabel格式的数据集操作总结。
聊聊自动化测试的度量指标
Hcip OSPF comprehensive experiment
【用C语言绘制谢尔宾斯基三角形】
[use SQLite3 library to realize student information management system in Visual Studio 2019]
HCIP oSPF综合实验
Multipoint bidirectional republication and routing strategy topology experiment
创业3年,现在鹅厂,年收入百万+,作为软件测试前辈的一些建议....
证券公司哪家手续费最低?手机上开户安全吗
Record the star user of handsomeblog
Go language slow start -- go operator
C language - assignment operator, compound assignment operator, self increasing and self decreasing operator, comma operator, conditional operator, goto statement, comment
Prometheus 运维工具 Promtool (三) Debug 功能
Array methods and loops in JS
【自此文之后,学习链表一片坦途】
Risc-v tool chain compilation notes
Static routing experiment configuration
Detailed source code of golang bufio reader
MGRE、PPP、HDLC综合实验
Hcip day 3 Wan topology experiment