当前位置:网站首页>Several common methods of multithreading
Several common methods of multithreading
2022-04-23 03:29:00 【Aiyou coffee】
Embodiment of multithreading , Yes thread,main Two threads run alternately
public class RunnableDemo implements Runnable{
@Override
public void run() {
for(int i = 1 ; i<10;i++){
System.out.println(Thread.currentThread().getName()+"--------Run---------"+i);
}
}
public static void main(String[] args) {
RunnableDemo runnableDemo = new RunnableDemo();
Thread thread = new Thread(runnableDemo);
thread.start();
for(int i = 0;i<10;i++){
System.out.println(Thread.currentThread().getName()+"---------main--------"+i );
}
}
}
Use stop Method
public class StopDemo {
public static void main(String[] args) {
RunDemo runDemo = new RunDemo();
Thread thread = new Thread();
thread.start();
for (int i = 0; i < 10; i++) {
if(i==5){
thread.stop();
}
System.out.println(Thread.currentThread().getName()+"----------"+i);
}
}
}
Use yield Method , Will give way once
public class YieldDemo {
public static void main(String[] args) {
RunDemo runDemo = new RunDemo();
Thread thread= new Thread(runDemo);
thread.start();
for (int i = 0; i < 10; i++) {
if(i==5){
// When i=5 When , Comity once
Thread.yield();
System.out.println(Thread.currentThread().getName()+" Giving way to ---------------"+i);
}else{
System.out.println(Thread.currentThread().getName()+"-------------------"+i);
}
}
}
}
Use Join Method , Will put one of the threads in a blocked state
public class JoinDemo {
public static void main(String[] args) {
RunDemo runDemo = new RunDemo();
Thread thread = new Thread();
thread.start();
for (int i = 0; i < 10; i++) {
if(i==3){
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"-----------------------------"+i);
}
}
}
Use sleep Method
public class SleepDemo {
public static void main(String[] args) {
RunDemo runDemo = new RunDemo();
Thread thread = new Thread(runDemo);
thread.start();
for (int i = 0; i < 10; i++) {
System.out.println(Thread.currentThread().getName() + "------------" + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
版权声明
本文为[Aiyou coffee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220619008730.html
边栏推荐
- Knowledge of software testing~
- 月薪10k-20k都无法回答的事务问题,你会吗?
- JS takes out the same elements in two arrays
- Chapter VI, Section III pointer
- Explication détaillée des fonctions send () et recv () du programme Socket
- Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
- PyMOL usage
- Generate QR code through zxing
- 第四次作业
- Explanation keyword of MySQL
猜你喜欢
L3-011 direct attack Huanglong (30 points)
MySQL索引详解【B+Tree索引、哈希索引、全文索引、覆盖索引】
淺學一下I/O流和File類文件操作
[vs Code] solve the problem that the jupyter file displays exceptions in vs code
Learn about I / O flow and file operations
Problem a: face recognition
Code forces round # 784 (DIV. 4) solution (First AK CF (XD)
MySQL之explain关键字详解
Optimization of especially slow startup in idea debugging mode
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
随机推荐
Redis(17) -- Redis缓存相关问题解决
Téléchargement en vrac de fichiers - téléchargement après compression
Cefsharp stores cookies and reads cookies
Do you really understand hashcode and equals???
Unity Basics
C abstract class
Using swagger in. Net5
7-1 introduction to finance
JS, bind the event for a label with input, and then bind the stand-alone event in the parent element. The event is executed twice and solved
File upload vulnerability summary and upload labs shooting range documentary
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
数据库表中不建索引,在插入数据时,通过sql语句防止重复添加(转载)
Idempotency practice operation, explaining idempotency based on business
Visual programming - drawing assignment
MySQL installation pit
Unity basics 2
Explication détaillée des fonctions send () et recv () du programme Socket
IDEA查看历史记录【文件历史和项目历史】
MySQL keyword group_ Concat, combined connection query
Problem a: face recognition