当前位置:网站首页>Specific use of multithreading
Specific use of multithreading
2022-07-27 03:54:00 【Half moon person】
First, let's take a look at the specific creation of multithreading :
First, there are many ways to create multithreads ->
1. Inherit Thread, rewrite
class MyThread extends Thread{
@Override
public void run() {
}
}
public class TextDemo {
public static void main(String[] args) {
Thread thread = new MyThread();
}
}
2. Realization 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. Inherit Thread, rewrite run, Use anonymous inner class
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(){
@Override
public void run() {
super.run();
}
};
}
}4. Realization Runable, rewrite run, Use internal expressions
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
}
});
}
}Use lambda expression
public class TextDemo {
public static void main(String[] args) {
Thread thread = new Thread(()->{
System.out.println("hello thread");
});
}
}The above is the basic thread creation method .
Thread In fact, the creation and construction methods of are not only added Runnable
In fact, it can also give Thread Add a name to , In order to make the program easy to debug threads
| Method | explain |
| Thread(String name) | Create a thread object and name |
| Thread(Running target,String name) | Use Running Create objects , And name |
Thread Common properties of

ID Is the displacement identifier of the thread , Different counties will not repeat
The name is used by various debugging tools
Status indicates that the ready-made is currently in a situation
Ready made with high priority is theoretically easier to schedule
About background threads , Need to remember a little :JVM After all background threads of a process end , Will end the operation .
Survival , Even a simple understanding , by run Whether the method has finished running
| attribute | Access method |
| ID | getid() |
| name | getNanme() |
| state | getState() |
| priority | getPriority() |
| Background thread or not | isDaemon() |
| Survival | isAlive() |
| Whether to interrupt | isInterrupted() |
How to interrupt a thread
1. Use custom variables as flag bits .
2. You need to give the flag to the merchant 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");
}Self setting flag bit .
example -> Use Thread.interrupted() perhaps Thre.currentThread().isInterrupted() Replace the custom flag bit
Thread Inside contains a Boolean The variable of type is used as a marker of whether the thread is interrupted
First, there are three ways :
| Method | explain |
| public void interrupt() | Interrupt the thread associated with the object , If the thread is blocking , Notify... In an abnormal way |
| public static boolean interrupted() | Judge whether the interrupt flag bit of the current thread is set , Clear the flag bit after calling . Thread “ Interrupt state ” Cleared by this method . let me put it another way , If the method is called twice in a row , The second call will return false. |
| public boolean isinterrupted() | Determine whether the flag bit of the associated thread is set , Do not clear the flag bit after calling |
thread There are two ways to receive notice :
1. If the thread is called wait/join/sleep And so on , with InterruptedException Notice in the form of exception , Clear interrupt flag
When there is a InterruptedException When , Whether you want to sleep in the thread depends on Catch How to write the code in . You can choose to ignore this exception , You can also jump out of the loop end thread .
2. otherwise , A break sign inside the pose is snake ,thread Can pass
Thread.interrupted() Determine whether the interrupt flag of the current thread is set , Clear interrupt flag .
Thread.currentThread().isinterrupted() Determine whether the interrupt flag of the specified thread is set , If you don't clear the interrupt flag, you will be notified in time , That is, the county is sleep You can also get it right away .
边栏推荐
- DataX无法连接对应的数据库(windows下可以,linux下失败)
- Briefly sort out the dualpivotquicksort
- 222. 完全二叉树的节点个数
- 477-82(236、61、47、74、240、93)
- 2022牛客多校第二场的J -- 三分做法
- Cocos game practice-04-collision detection and NPC rendering
- Implementation of API short message gateway based on golang
- Banyan data model of Bairong
- Explain tool actual operation
- 477-82(236、61、47、74、240、93)
猜你喜欢

【安卓小叙】Kotlin多线程编程(一)

数字孪生应用及意义对电力的主要作用,概念价值。

Redis spike case, learn from Shang Silicon Valley teacher in station B

On the first day of Shenzhen furniture exhibition, the three highlights of Jin Ke'er booth were unlocked!

一维数组的应用

MySQL中文失败问题

Learning and understanding of four special data types of redis

Kettle reads file split by line

深圳家具展首日,金可儿展位三大看点全解锁!

Source code analysis of openfeign
随机推荐
Detailed tutorial of typera
MySQL中文失败问题
Process analysis of object creation
How to conduct 360 assessment
Add support for @data add-on in idea
OC message mechanism
Basic concept and essence of Architecture
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
Number of 0 at the end of factorial
Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image
Connman introduction
Source code analysis of openfeign
智能体重秤方案主控采用CSU18M91
Kettle读取按行分割的文件
Introduction to redis
Use websocket to realize a web version of chat room (fishing is more hidden)
这个FlinkCDC会监控数据库中所有的表?还是指定的表呢?我看后台日志,他是监控了所有表,如果监控
typescript ts 基础知识之接口、泛型
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
若依框架代码生成详解