当前位置:网站首页>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 .
边栏推荐
- 基于OpenCV的轮廓检测(2)
- What are "full five unique" and "full two unique"? Any difference?
- Basic concept and essence of Architecture
- J-3-point practice in the second game of 2022 Niuke multi school
- Redis源码学习(33),命令执行过程
- Program to change the priority of the process in LabVIEW
- Banyan data model of Bairong
- URDF_Xcaro
- LPCI-252通用型PCI接口CAN卡的功能和应用介绍
- 768. 最多能完成排序的块 II 贪心
猜你喜欢

Network security / penetration testing tool awvs14.9 download / tutorial / installation tutorial

Chapter 5 决策树和随机森林实践

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Use websocket to realize a web version of chat room (fishing is more hidden)
![Machine learning [Matplotlib]](/img/d1/31ec2f96ca96465c6863798c7db179.jpg)
Machine learning [Matplotlib]

关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案

connman介绍

222. 完全二叉树的节点个数

How to optimize MySQL

飞腾腾锐 D2000 荣获数字中国“十大硬核科技”奖
随机推荐
How can you access the domestic server and overseas server quickly with one database?
How to interact with the server when the client sends an SQL message
It's confirmed that the registration of soft exam in the second half of 2022 will start in August
榕树贷款C语言结构体里的成员数组和指针
How to optimize MySQL
明汯投资裘慧明:长期优异超额的背后考验的是团队的投研能力和策略的完整性
DNS record type and explanation of related terms
222. 完全二叉树的节点个数
百融榕树数据模型
Kettle reads file split by line
一维数组的应用
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Spark: ranking statistics of regional advertising hits (small case)
Vector to SVG method
Solution to Chinese garbled code in console header after idea connects to database to query data
Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
C语言入门实战(12):求自然常数e的值
Installation and use of anti-virus software ClamAV
Realization of regular hexagon map with two-dimensional array of unity
Redis源码学习(33),命令执行过程