当前位置:网站首页>Episode 3: thread synchronization using thread lock
Episode 3: thread synchronization using thread lock
2022-06-25 23:29:00 【Guitingting】
Catalog
Programming requirements
Please read the code on the right , Follow the tips in the method , stay Begin - End
Code supplement within the area .
Test instructions
Make the program output the following results ( Because the execution order of threads is random, you may need to evaluate it many times ):
Thread-0 Got the lock.
1
2
3
4
5
Thread-0 Lock released
Thread-1 Got the lock.
6
7
8
9
10
Thread-1 Lock released
Thread-2 Got the lock.
11
12
13
14
15
Thread-2 Lock released
This level is a bit of a pit !!!
The following is the code I wrote. It can pass the self-test only once !
Main idea
When thread one runs , Let the main thread take a break , Wait for the child thread to finish executing , Start other threads .
Code implementation
package step3;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Task {
public static void main(String[] args) {
final Insert insert = new Insert();
Thread t1 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
insert.insert(Thread.currentThread());
}
});
// Set thread priority
// t1.setPriority(Thread.MAX_PRIORITY);
// t2.setPriority(Thread.NORM_PRIORITY);
// t3.setPriority(Thread.MIN_PRIORITY);
t1.start();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
}
}
class Insert {
public static int num;
// Defined here Lock
private Lock lock = new ReentrantLock();
public void insert(Thread thread) {
/********* Begin *********/
if(lock.tryLock()){
try{
System.out.println(thread.getName()+" Got the lock. ");
for (int i = 0; i < 5; i++) {
num++;
System.out.println(num);
}
}finally{
System.out.println(thread.getName()+" Lock released ");
lock.unlock();
}
}else{
System.out.println(thread.getName()+" Lock acquisition failed ");
}
}
/********* End *********/
}
边栏推荐
- 电路模块分析练习6(开关)
- After xampp restarts, the MySQL service cannot be started.
- PDM fur
- Paper notes: multi tag learning MSWl
- konva系列教程2:绘制图形
- How to use drawing comparison function in CAD
- UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
- Oracle -- table operation
- Actual combat: how to quickly change font color in typera (blog sharing - perfect) -2022.6.25 (solved)
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
猜你喜欢
What is Unified Extensible Firmware Interface (UEFI)?
[modulebuilder] GP service realizes the intersection selection of two layers in SDE
ES6 - numerical extension and object extension
2. What is the geometric meaning of a vector multiplying its transpose?
Konva series tutorial 2: drawing graphics
指针强化与提高
Qt 中文和英文分别使用不同的字体
UE4\UE5 蓝图节点Delay与Retriggerable Delay的使用与区别
Multithreaded learning 2- call control
ES6 learning -- let
随机推荐
指针强化与提高
Live800 online customer service system: do business across time and space, starting from each interaction
分享一个OSGeo4W64下载好的库,基于qgis3.10的
为什么OpenCV计算的帧率是错误的?
Efr32bg22 ble module (low power Bluetooth communication module) at command test
最近准备翻译外国优质文章
Leetcode(435)——无重叠区间
UE4_UE5结合offline voice recognition插件做语音识别功能
ACM. HJ16 购物单 ●●
Idea auto generator generates constructor get/set methods, etc
Oracle - data query
我的vscode
Svn icon disappearing solution
OpenJudge NOI 2.1 15:Counterfeit Dollar
RK3568+鸿蒙工控板工业网关视频网关解决方案
Repoptimizer: it's actually repvgg2
No absurd tea applet - rule change
Pycharm student's qualification expires, prompting no suitable licenses associated with account solution
Meta universe standard forum established
建立自己的网站(15)