当前位置:网站首页>How to create a thread
How to create a thread
2022-07-05 22:19:00 【Love to knock code high】
Catalog
Two : How to construct threads
1: Inherit Thread class , rewrite run Method
2: Realization runnable Interface , rewrite run Method
3: Use lambda Expression creation runnable Subclass object
4 Anonymous inner class methods
One : Recognize threads
When it comes to threads , We have to mention the process . A more official definition , A process is the smallest unit of resources allocated by a system , This resource can be cpu、 Memory, etc . Thread is the smallest unit of system scheduling . And all threads in the same process can share resources with each other .
say concretely , for instance , Our mother goes out to buy vegetables at noon , Then buying vegetables can be regarded as a process , Then mom asked us to sell fish , She went to buy meat , Selling fish and meat here are two threads in the process of buying vegetables , Then some corresponding resources , For example, the money used to buy vegetables , You can use it when buying fish and meat . And doing it separately is better than buying meat after buying fish with my mother , The relative speed is very fast .
In this , We found that , Multithreading can improve efficiency .
Two : How to construct threads
1: Inherit Thread class , rewrite run Method
The code is as follows
class MyThread extends Thread{
@Override
public void run() {
System.out.println("hello,Thread");
}
}
public class ThreadDemo1 {
public static void main(String[] args) {
Thread t =new MyThread();
t.start();
t.run();
}
}
2: Realization runnable Interface , rewrite run Method
The code is as follows
class MyRunnable implements Runnable{
@Override
public void run() {
while (true){
System.out.println("hello,Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadDemo3 {
public static void main(String[] args) {
Thread t =new Thread(new MyRunnable());
t.start();
}
}
3: Use lambda Expression creation runnable Subclass object
The code is as follows
public class ThreadDemo6 {
public static void main(String[] args) {
Thread t =new Thread(() ->{
while (true){
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}
}
Besides , You can also use anonymous inner classes to create Thread and Runnable object
4 Anonymous inner class methods
1:Thread
public class ThreadDemo4 {
public static void main(String[] args) {
// This syntax is the anonymous inner class
// It is equivalent to creating an anonymous class , This class inherits Thread
// Here we new Example , In fact, that is new An instance of this new subclass
Thread t =new Thread(){
@Override
public void run() {
while(true){
System.out.println("hello,Thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}
2:Runnable
public class ThreadDemo5 {
public static void main(String[] args) {
Thread t =new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("hello thread"); try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
}
The above are some differences and connections between thread processes and several methods of creating threads , There are five kinds .
边栏推荐
- A trip to Suzhou during the Dragon Boat Festival holiday
- boundary IoU 的计算方式
- Livelocks and deadlocks of concurrency control
- 科技云报道:算力网络,还需跨越几道坎?
- CA certificate trampled pit
- How can Bluetooth in notebook computer be used to connect headphones
- Alternating merging strings of leetcode simple questions
- Official clarification statement of Jihu company
- Common interview questions of JVM manufacturers
- Cobaltstrike builds an intranet tunnel
猜你喜欢
50. Pow(x, n). O(logN) Sol
Nacos installation and service registration
点到直线的距离直线的交点及夹角
Recovery technology with checkpoints
Web3为互联网带来了哪些改变?
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
K210 learning notes (IV) k210 runs multiple models at the same time
Installation of VMware Workstation
随机推荐
Hcip day 16
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Nacos 的安装与服务的注册
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Blocking protocol for concurrency control
A number of ventilator giants' products have been recalled recently, and the ventilator market is still in incremental competition
Official clarification statement of Jihu company
点到直线的距离直线的交点及夹角
90后测试员:“入职阿里,这一次,我决定不在跳槽了”
实战:fabric 用户证书吊销操作流程
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
Oracle hint understanding
Pl/sql basic syntax
The new content of the text component can be added through the tag_ Config set foreground and background colors
The real situation of programmers
IIC bus realizes client device
如何创建线程
How to quickly experience oneos
Leetcode simple question: check whether each row and column contain all integers