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

56
LINES

< > BotCompany Repo | #1009313 // Try to solve ReentrantLock deadlock by interrupting threads when using lock() [fails]

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.lock();
23  
    try {
24  
      print("Thread A has lock A.");
25  
      sleep1Second();
26  
      print("Thread A getting lock B.");
27  
      b.lock();
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.lock();
40  
    try {
41  
      print("Thread B has lock B.");
42  
      sleep1Second();
43  
      print("Thread B getting lock A.");
44  
      a.lock();
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 #1009302

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: #1009313
Snippet name: Try to solve ReentrantLock deadlock by interrupting threads when using lock() [fails]
Eternal ID of this version: #1009313/4
Text MD5: df3ea8d52db02aca7116079369a22db2
Transpilation MD5: daad3ed1c89a50aef67d2912bddc0e7a
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:24:03
Source code size: 1147 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 522 / 585
Version history: 3 change(s)
Referenced in: [show references]