当前位置:网站首页>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
边栏推荐
- . net 5 Web custom middleware implementation returns the default picture
- Five tips for cross-border e-commerce in 2022
- The fourth operation
- "Visual programming" test paper
- 通过 zxing 生成二维码
- Scenario Title: how does system a use the page of system B
- Visual programming -- how to customize the mouse cursor
- What to pay attention to when writing the first code
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- 超好用的【通用Excel导入功能】
猜你喜欢

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

深度学习笔记(二)——激活函数原理与实现

Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types

Code forces round # 784 (DIV. 4) solution (First AK CF (XD)

TCP three handshakes and four waves

Build websocket server in. Net5 webapi

Applet - canvas drawing Poster

Supersocket is Used in net5 - command

Supersocket is Use in net5 - concept

Unity knowledge points (ugui)
随机推荐
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
Do you really understand hashcode and equals???
Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
Using jsonserialize to realize data type conversion gracefully
. net webapi access authorization mechanism and process design (header token + redis)
js 中,为一个里面带有input 的label 绑定事件后在父元素绑定单机事件,事件执行两次,求解
Peut recevoir plusieurs paramètres de type de données - paramètres variables
Oracle query foreign keys contain comma separated data
Supersocket is Use in net5 - startup
Redis (17) -- redis cache related problem solving
JS - accuracy issues
Flink real-time data warehouse project - Design and implementation of DWS layer
Idempotency practice operation, explaining idempotency based on business
Build websocket server in. Net5 webapi
oracle 查询外键含有逗号分隔的数据
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
The fourth operation
WinForm allows the form form to switch between the front and active states
PyMOL usage
7-3 poly width