当前位置:网站首页>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
边栏推荐
- The principle and solution of not allowing pasting in an English Network
- Translation of l1-7 matrix columns in 2022 group programming ladder Simulation Competition (20 points)
- 打卡:4.23 C语言篇 -(1)初识C语言 - (12)结构体
- Iotos IOT middle platform is connected to the access control system of isecure center
- Codeforces Round #784 (Div. 4)題解 (第一次AK cf (XD
- C abstract class
- Optimization of especially slow startup in idea debugging mode
- 2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
- C-11 problem h: treasure chest 2
- Docker拉取mysql并连接
猜你喜欢

Why is bi so important to enterprises?

C set

Visual programming - Experiment 1

"Visual programming" test paper

Database SQL -- simulate inserting a large amount of data, importing / exporting database scripts, timestamp conversion and database basics

A comprehensive understanding of static code analysis
![[vs Code] solve the problem that the jupyter file displays exceptions in vs code](/img/f6/a91d03fd140eb5f7688b72e2e6f2bb.png)
[vs Code] solve the problem that the jupyter file displays exceptions in vs code

Is it difficult to choose binary version control tools? After reading this article, you will find the answer

Redis(17) -- Redis缓存相关问题解决

2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
随机推荐
Problem C: Hanoi Tower III
Utgard connection opcserver reported an error caused by: org jinterop. dcom. common. JIRuntimeException: Access is denied. [0x800
2022 团体程序设计天梯赛 模拟赛 L2-4 哲哲打游戏 (25 分)
Section 2 map and structure in Chapter 6
场景题:A系统如何使用B系统的页面
WinForm allows the form form to switch between the front and active states
2022 group programming ladder simulation l2-1 blind box packaging line (25 points)
MySQL query specifies that a row is sorted to the first row
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Supersocket is Use in net5 - concept
List interface of collection
Scenario Title: how does system a use the page of system B
第四次作业
JS implementation of new
Five tips for cross-border e-commerce in 2022
The fourth operation
打卡:4.22 C语言篇 -(1)初识C语言 - (11)指针
Oracle query foreign keys contain comma separated data
Visual programming - Experiment 2
Test questions and some space wars