当前位置:网站首页>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 .
边栏推荐
- How to view Apache log4j 2 remote code execution vulnerability?
- Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
- [groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)
- Decorator learning 01
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- The statistics of leetcode simple question is the public string that has appeared once
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
- Calculation method of boundary IOU
- [groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
- 科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
猜你喜欢
The difference between MVVM and MVC
Leetcode simple question check whether all characters appear the same number of times
Nacos 的安装与服务的注册
AD637使用筆記
Create a virtual machine on VMware (system not installed)
How to quickly experience oneos
How to view Apache log4j 2 remote code execution vulnerability?
The simple problem of leetcode is to split a string into several groups of length K
How can Bluetooth in notebook computer be used to connect headphones
Blocking of concurrency control
随机推荐
Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
New 3D particle function in QT 6.3
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
点到直线的距离直线的交点及夹角
数据泄露怎么办?'华生·K'7招消灭安全威胁
Cobaltstrike builds an intranet tunnel
Analysis of the problem that the cookie value in PHP contains a plus sign (+) and becomes a space
Recovery technology with checkpoints
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
Kubernetes Administrator certification (CKA) exam notes (IV)
A substring with a length of three and different characters in the leetcode simple question
700. Search in a Binary Search Tree. Sol
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
Leetcode simple question: the minimum cost of buying candy at a discount
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Damn, window in ie open()
Nacos installation and service registration
Type of fault
AD637使用筆記
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)