当前位置:网站首页>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 .
边栏推荐
- Create a virtual machine on VMware (system not installed)
- The new content of the text component can be added through the tag_ Config set foreground and background colors
- Oracle advanced query
- Daily question brushing record (XIV)
- A long's perception
- Microservice link risk analysis
- AD637 usage notes
- The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
- Business learning of mall order module
- 90后测试员:“入职阿里,这一次,我决定不在跳槽了”
猜你喜欢

The real situation of programmers

Index optimization of performance tuning methodology

Daily question brushing record (XIV)

Nacos 的安装与服务的注册

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

Pl/sql basic case

Interview questions for famous enterprises: Coins represent a given value

opencv 判断点在多边形内外

What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial

Storage optimization of performance tuning methodology
随机推荐
Database recovery strategy
Three "factions" in the metauniverse
1.3 years of work experience, double non naked resignation agency face-to-face experience [already employed]
GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
The new content of the text component can be added through the tag_ Config set foreground and background colors
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Character conversion PTA
[Yugong series] go teaching course in July 2022 004 go code Notes
元宇宙中的三大“派系”
如何创建线程
Recovery technology with checkpoints
如何快速体验OneOS
Search: Future Vision (moving sword)
科技云报道:算力网络,还需跨越几道坎?
FBO and RBO disappeared in webgpu
The real situation of programmers
Leetcode simple question check whether all characters appear the same number of times
Shelved in TortoiseSVN- Shelve in TortoiseSVN?
笔记本电脑蓝牙怎么用来连接耳机