当前位置:网站首页>Introduction to concurrent programming (II)
Introduction to concurrent programming (II)
2022-07-03 12:11:00 【zwanying】
Introduction to concurrent programming ( Two )
Thread join Method
purpose
A thread has finished executing , To implement the subsequent methods .
abnormal
join Method Society throws InterruptedException, So when using , Or capture , Or throw .
Use
establish 3 Threads , etc. 3 All threads execute and then respond .
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable(){
@Override
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"sleep 1s");
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run(){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"sleep 5s");
}
});
Thread t3 = new Thread(new Runnable(){
@Override
public void run(){
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread()+"sleep 8s");
}
});
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("end");
}
Don't use join Method , First, the output “end” , Then the output Threads 1,2,3 Implementation .
Use join after , Only after the thread execution is completed will it output “end”. It's a bit like setting parameters to synchronous when asynchronous .
Parametric join Method
join( Number of milliseconds ) n Wait no longer after milliseconds .
Thread yield Method
purpose
humility , Not used up CPU Allocated time , Give up voluntarily CPU Time , For other threads with the same priority .
characteristic
static Static ,Thread.yield() Call directly .
native Local , Call not Java Methods of language implementation , Operating system .
Use
Create a thread , perform 1-10000 Accumulation , Join in yield Method , Slow execution .
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable(){
@Override
public void run(){
int sum = 0;
for(int i=0;i<10000000;i++){
sum = sum + i;
Thread.yield();
}
}
});
long cur = System.currentTimeMillis();
t1.start();
t1.join();
System.out.println(System.currentTimeMillis()-cur);
}
sleep() yield() difference
sleep() Method , Thread blocking , Allow all other threads to execute first , It throws an exception , Good portability .
yield() Method , Thread ready , Only threads with the same or higher priority are allowed to execute first , Don't throw exceptions .
Context switching and deadlock
Context switch
One thread CPU The allotted time slice has run out , Or interrupted , Get ready , You need to save the execution status , Give up CPU Execute... To other threads , This is the context switch .
thread deadlock :
Two or more threads , Waiting for each other for resources , Without external force , Will wait forever .
Necessary conditions for deadlock
Mutually exclusive : A resource can only be occupied by the same process for a period of time
Inalienable : Cannot be forcibly terminated .
Request and hold : Occupy a resource , Request another resource
Loop waiting for
Deadlock implementation
public static void main(String[] args) throws InterruptedException {
Object a = 1;
Object b = 2;
Thread t1 = new Thread(new Runnable(){
@Override
public void run(){
synchronized (a){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("1 -> b" );
synchronized (b){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("end" );
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run(){
synchronized (b){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("2 -> a" );
synchronized (a){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("end" );
}
});
t1.start();
t2.start();
}
Destruction requirement , Avoid deadlock
Only request holding and circular waiting can be broken , above , By adjusting the order of obtaining resources , All are First get a, Get more b, Avoid circular waiting , Can avoid deadlock .
边栏推荐
- 4000 word super detailed pointer
- QT OpenGL texture map
- Oracle advanced (I) realize DMP by expdp impdp command
- Introduction to the implementation principle of rxjs observable filter operator
- XML (DTD, XML parsing, XML modeling)
- temp
- php 获取文件夹下面的文件列表和文件夹列表
- Dart: about Libraries
- Use of QT OpenGL camera
- Unicode encoding table download
猜你喜欢
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

小鹏 P7 撞护栏安全气囊未弹出,官方回应称撞击力度未达到弹出要求
![[official MySQL document] deadlock](/img/2d/04e97d696f20c2524701888ea9cd10.png)
[official MySQL document] deadlock

网络通讯之Socket-Tcp(一)

(构造笔记)ADT与OOP

Momentum of vulnhub

Shutter: add gradient stroke to font

Unicode encoding table download

Is BigDecimal safe to calculate the amount? Look at these five pits~~

OpenGL index cache object EBO and lineweight mode
随机推荐
Vulnhub's Nagini
Qt+vtk+occt reading iges/step model
Talk about the state management mechanism in Flink framework
vulnhub之momentum
安裝electron失敗的解决辦法
Solve msvcp120d DLL and msvcr120d DLL missing
Php Export word method (One MHT)
4000 word super detailed pointer
(构造笔记)ADT与OOP
Socket TCP for network communication (I)
Notes on 32-96 questions of sword finger offer
Qt OpenGL 纹理贴图
Dart: view the dill compiled code file
Is BigDecimal safe to calculate the amount? Look at these five pits~~
vulnhub之presidential
Simple factory and factory method mode
Unicode encoding table download
Shardingsphere sub database and sub table < 3 >
Use of QT OpenGL camera
Niuniu's team competition