当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
242. Effective letter heteronyms
网络通讯之Socket-Tcp(一)
【mysql专项】读锁和写锁
XML (DTD, XML parsing, XML modeling)
[learning notes] DP status and transfer
Groovy test class and JUnit test
Shardingsphere sub database and sub table < 3 >
Vulnhub's Nagini
Dynamically monitor disk i/o with ZABBIX
Groovy测试类 和 Junit测试
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
MySQL searches and sorts out common methods according to time
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
Dart: About zone
Wechat applet - basic content
Shutter: about inheritedwidget
Wechat applet development - page Jump transfer parameters
(construction notes) learning experience of MIT reading
STL tutorial 10 container commonalities and usage scenarios
Fluent: Engine Architecture