当前位置:网站首页>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 .
边栏推荐
- 极狐公司官方澄清声明
- Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
- [error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
- Oracle hint understanding
- 如何開發引入小程序插件
- FBO and RBO disappeared in webgpu
- Alternating merging strings of leetcode simple questions
- Ad637 notes d'utilisation
- 航海日答题小程序之航海知识竞赛初赛
- ESP32 hosted
猜你喜欢

Business learning of mall order module

MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

Index optimization of performance tuning methodology

Create a virtual machine on VMware (system not installed)

Search: Future Vision (moving sword)

Blocking of concurrency control

Implementation technology of recovery

2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth

Pl/sql basic syntax
随机推荐
Oracle views the data size of a table
如何开发引入小程序插件
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
极狐公司官方澄清声明
Leetcode simple question: check whether each row and column contain all integers
What about data leakage? " Watson k'7 moves to eliminate security threats
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
A long's perception
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
Type of fault
Stored procedures and stored functions
Decorator learning 01
Kubernetes Administrator certification (CKA) exam notes (IV)
The statistics of leetcode simple question is the public string that has appeared once
装饰器学习01
Overriding equals() & hashCode() in sub classes … considering super fields
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
Implementation technology of recovery
How to view Apache log4j 2 remote code execution vulnerability?
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~