Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1009314 // Try to solve ReentrantLock deadlock by interrupting threads when using lockInterruptibly() [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (1682L/11K/43K).

1  
!7
2  
3  
static Lock a = fairLock();
4  
static Lock b = fairLock();
5  
6  
p-tt {
7  
  centerHigherConsole();
8  
  
9  
  doEvery_daemon(1000, r {
10  
    L<ThreadInfo> lockingThreads = printDeadlocks();
11  
    if (nempty(lockingThreads)) {
12  
      print("Interrupting locking threads.");
13  
      L<Thread> threads = threadInfosToThreads(lockingThreads);
14  
      print("Interrupting " + n(threads, "thread"));
15  
      for (Thread t : threads) t.interrupt();
16  
    }
17  
  });
18  
  
19  
  print("Initiating deadlock... :-)\n");
20  
  
21  
thread "A" {
22  
    a.lockInterruptibly();
23  
    try {
24  
      print("Thread A has lock A.");
25  
      sleep1Second();
26  
      print("Thread A getting lock B.");
27  
      b.lockInterruptibly();
28  
      try {
29  
        print("Thread A successful!");
30  
      } finally {
31  
        b.unlock();
32  
      }
33  
    } finally {
34  
      a.unlock();
35  
    }
36  
  }
37  
  
38  
  thread "B" {
39  
    b.lockInterruptibly();
40  
    try {
41  
      print("Thread B has lock B.");
42  
      sleep1Second();
43  
      print("Thread B getting lock A.");
44  
      a.lockInterruptibly();
45  
      try {
46  
        print("Thread B successful!");
47  
      } finally {
48  
        a.unlock();
49  
      }
50  
    } finally {
51  
      b.unlock();
52  
    }
53  
  }
54  
 
55  
  sleepQuietly();
56  
}

Author comment

Began life as a copy of #1009313

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1009314
Snippet name: Try to solve ReentrantLock deadlock by interrupting threads when using lockInterruptibly() [WORKS]
Eternal ID of this version: #1009314/3
Text MD5: beb85538f20fb5097ab4a3b9a38c34a8
Transpilation MD5: bdd3b97333ad9a478aadbb81c328d644
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-23 15:26:32
Source code size: 1199 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 493 / 543
Version history: 2 change(s)
Referenced in: [show references]