Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
40 lines (32 loc) · 1.2 KB

File metadata and controls

40 lines (32 loc) · 1.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ThreadTest;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
* 题目:不使用Synchronized 实现互斥量
* */
class SynchronizedClass{
/*
* 在线程对其进行共享资源进行使用的时候,JVM负责跟踪对象被加锁的次数,当一个任务第一次使用资源时,count=1,
* 此时次任务如果再继续使用此资源时,count++ 当对资源的每一次使用结束时,count--,直到count=0,此时说明
* 该资源解锁,完全被释放
* */
private static int count=0;
/*
*使用Lock对象显式实现互斥机制,比Synchronized关键字使用的代码多,只有在解决特殊问题时,才会使用Lock对象
* 比如去解决:尝试在一段时间内去获得锁,可以在一段时间内获得成功,也可以失败
* Lock类提高了细粒度的控制力
* */
private int CurrentNum=0;
private Lock lock=new ReentrantLock();
public int next(){
lock.lock(); //上锁
try{
CurrentNum++;
return CurrentNum;
}finally {
lock.unlock(); //任务完成,加锁
}
}
}
public class SynchronizedTest {
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.