当前位置:网站首页>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 .
边栏推荐
- DNS multi-point deployment IP anycast+bgp actual combat analysis
- ES6新特性
- [MySQL special] read lock and write lock
- Develop plug-ins for idea
- win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
- previous permutation lintcode51
- PHP导出word方法(一mht)
- pragma-pack语法与使用
- Redis notes 01: Introduction
- OpenGL 着色器使用
猜你喜欢
随机推荐
Use of QT OpenGL camera
(construction notes) learning experience of MIT reading
Momentum of vulnhub
vulnhub之pyexp
SystemVerilog -- OOP -- copy of object
Experience container in libvirt
Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
vulnhub之Ripper
vulnhub之raven2
Kubernetes three dozen probes and probe mode
OpenGL 绘制彩色的三角形
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
Dart: About zone
XML (DTD, XML parsing, XML modeling)
Test classification in openstack
Why can't my MySQL container start
Colleagues wrote a responsibility chain model, with countless bugs
MySQL searches and sorts out common methods according to time
win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法
shardingSphere分库分表<3>









