forked from txs72/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample01.java
More file actions
executable file
·49 lines (47 loc) · 1.52 KB
/
Example01.java
File metadata and controls
executable file
·49 lines (47 loc) · 1.52 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
/* */ package multithreads;
/* */
/* */ import java.util.ArrayList;
/* */ import java.util.concurrent.locks.Lock;
/* */ import java.util.concurrent.locks.ReentrantLock;
/* */
/* */
/* */ public class Example01
/* */ {
/* 10 */ private ArrayList<Integer> arrayList = new ArrayList<>();
/* 11 */ Lock lock = new ReentrantLock();
/* */ public static void main(String[] args) {
/* 13 */ final Example01 test = new Example01();
/* */
/* 15 */ (new Thread() {
/* */ public void run() {
/* 17 */ test.insert(Thread.currentThread());
/* */ }
/* 19 */ }).start();
/* */
/* 21 */ (new Thread() {
/* */ public void run() {
/* 23 */ test.insert(Thread.currentThread());
/* */ }
/* 25 */ }).start();
/* */ }
/* */
/* */
/* */ public void insert(Thread thread) {
/* 30 */ this.lock.lock();
/* */ try {
/* 32 */ System.out.println(thread.getName() + "得到了锁");
/* 33 */ for (int i = 0; i < 5; i++) {
/* 34 */ this.arrayList.add(Integer.valueOf(i));
/* */ }
/* 36 */ } catch (Exception exception) {
/* */
/* */ } finally {
/* 39 */ System.out.println(thread.getName() + "释放了锁");
/* 40 */ this.lock.unlock();
/* */ }
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/multithreads/Example01.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/