当前位置:网站首页>多线程的具体使用
多线程的具体使用
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也可以马上手到.
边栏推荐
- How many holes have you stepped on in BigDecimal?
- Find a specific number in an ordered array
- Redis distributed lock implemented by annotation
- NAT network address translation protocol topology experiment
- Go language slow start -- go operator
- OSPF summary (mind map)
- 静态路由综合实验
- OSPF路由信息协议-拓扑实验
- MGRE, PPP, HDLC comprehensive experiment
- 聊聊自动化测试的度量指标
猜你喜欢
随机推荐
毕业进入HW,从测试工程师到项目经理,现如今在鹅厂年收入百万,我的给大家的一些建议...
f8抓交通、f9抓兔子、f10turtle
Open the door of programming
NAT网络地址转化实验
全连MGRE与星型拓扑MGRE
【你了解Cache吗——全面理解高速缓冲存储器】
见证中国网安力量 “解码2022中国网安强星”即将启航
Record the star user of handsomeblog
Redis安装及运行(linux)
C language -- while statement, dowhile statement, for loop and loop structure, break statement and continue statement
Detailed source code of golang bufio reader
C language - first program, print, variables and constants
【斐波那契数列及螺线 基于C语言】
Array methods and loops in JS
打开编程的大门
NAT network address translation protocol topology experiment
Use of golang - sync package (waitgroup, once, mutex, rwmutex, cond, pool, map)
【Code】剑指offer 04二维数组中的查找
NPM reports an error, error: eperm: operation not permitted, MKDIR
What is the principle of synchronized lock escalation in multithreading?









