当前位置:网站首页>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. 12345Thread-0 Lock released Thread-1 Got the lock. 678910Thread-1 Lock released Thread-2 Got the lock. 1112131415Thread-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 *********/
}
边栏推荐
- UE4 学习记录二 给角色添加骨架,皮肤,及运动动画
- Oracle -- table operation
- Ue4 Ue5 combine le plug - in de reconnaissance vocale de bureau pour la reconnaissance vocale
- CTS RTS RX TX in serial port flow control UART (direct communication between serial port module and MCU)
- 2. What is the geometric meaning of a vector multiplying its transpose?
- The first public available pytorch version alphafold2 is reproduced, and Columbia University is open source openfold, with more than 1000 stars
- Kubernetes cluster construction of multiple ECS
- Recently prepared to translate foreign high-quality articles
- Oracle - data query
- Leetcode(605)——种花问题
猜你喜欢
![[modulebuilder] GP service realizes the intersection selection of two layers in SDE](/img/4a/899a3c2a0505d2ec2eaae97a3948c9.png)
[modulebuilder] GP service realizes the intersection selection of two layers in SDE

What is Unified Extensible Firmware Interface (UEFI)?

CAD中图纸比较功能怎么用

电路模块分析练习6(开关)

Idea auto generator generates constructor get/set methods, etc

【AXI】解读AXI协议原子化访问

RK3568+鸿蒙工控板工业网关视频网关解决方案

Oracle - getting started

Beacon realizes asset management and indoor positioning based on 5.2 ultra-low power Bluetooth module efr32 (bg22ax)

QComboBox下拉菜单中有分隔符Separator时的样式设置
随机推荐
STM32开发板+机智云AIoT+家庭监测控制系统
Circuit module analysis exercise 5 (power supply)
character string
Idea common plug-ins
【ModuleBuilder】GP服务实现SDE中两个图层相交选取
leetcode_ 136_ A number that appears only once
Qt 中文和英文分别使用不同的字体
How to add cartoon characters to the blog park?
Es6-- set
Set up your own website (15)
电路模块分析练习5(电源)
剑指 Offer 46. 把数字翻译成字符串(DP)
漏刻有时API接口实战开发系列(13):小鹅通云服务PHP-API二维数组传参解决方案
Go语言逃逸分析全纪录
Svn icon disappearing solution
Oracle - data query
The applet draws a simple pie chart
论文笔记: 多标签学习 MSWL
Implementation of sequence table: static and dynamic
关于go中资源泄漏/goroutine泄漏/内存泄漏/CPU打满等情况分析