forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample03.java
More file actions
executable file
·55 lines (53 loc) · 1.64 KB
/
Example03.java
File metadata and controls
executable file
·55 lines (53 loc) · 1.64 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* */ package multithreads;
/* */
/* */ import java.util.concurrent.locks.Lock;
/* */ import java.util.concurrent.locks.ReentrantLock;
/* */
/* */
/* */
/* */
/* */
/* */
/* */
/* */ public class Example03
/* */ {
/* 14 */ private Lock lock = new ReentrantLock();
/* */
/* */ public static void main(String[] args) {
/* 17 */ Example03 test = new Example03();
/* 18 */ MyThread thread1 = new MyThread(test);
/* 19 */ MyThread thread2 = new MyThread(test);
/* 20 */ thread1.start();
/* 21 */ thread2.start();
/* */
/* */ try {
/* 24 */ Thread.sleep(2000L);
/* 25 */ } catch (InterruptedException e) {
/* 26 */ e.printStackTrace();
/* */ }
/* 28 */ thread2.interrupt();
/* */ }
/* */
/* */ public void insert(Thread thread) throws InterruptedException {
/* 32 */ this.lock.lockInterruptibly();
/* */
/* */
/* */ try {
/* 36 */ System.out.println(thread.getName() + "得到了锁");
/* 37 */ long startTime = System.currentTimeMillis(); do {
/* */
/* 39 */ } while (System.currentTimeMillis() - startTime < 10000L);
/* */
/* */ }
/* */ finally {
/* */
/* 44 */ System.out.println(Thread.currentThread().getName() + "执行finally");
/* 45 */ this.lock.unlock();
/* 46 */ System.out.println(thread.getName() + "释放了锁");
/* */ }
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/multithreads/Example03.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/