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

59
LINES

< > BotCompany Repo | #1009315 // Try to solve ReentrantLock deadlock by interrupting a random thread when using lockInterruptibly() [WORKS]

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

Libraryless. Click here for Pure Java version (3091L/19K/76K).

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  
      if (nempty(threads)) {
15  
        Thread t = random(threads);
16  
        print("Interrupting thread " + quote(t.getName()));
17  
        t.interrupt();
18  
      }
19  
    }
20  
  });
21  
  
22  
  print("Initiating deadlock... :-)\n");
23  
  
24  
thread "A" {
25  
    lock(a);
26  
    try {
27  
      print("Thread A has lock A.");
28  
      sleep1Second();
29  
      print("Thread A getting lock B.");
30  
      lock(b);
31  
      try {
32  
        print("Thread A successful!");
33  
      } finally {
34  
        b.unlock();
35  
      }
36  
    } finally {
37  
      a.unlock();
38  
    }
39  
  }
40  
  
41  
  thread "B" {
42  
    lock(b);
43  
    try {
44  
      print("Thread B has lock B.");
45  
      sleep1Second();
46  
      print("Thread B getting lock A.");
47  
      lock(a);
48  
      try {
49  
        print("Thread B successful!");
50  
      } finally {
51  
        a.unlock();
52  
      }
53  
    } finally {
54  
      b.unlock();
55  
    }
56  
  }
57  
 
58  
  sleepQuietly();
59  
}

Author comment

Began life as a copy of #1009314

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: #1009315
Snippet name: Try to solve ReentrantLock deadlock by interrupting a random thread when using lockInterruptibly() [WORKS]
Eternal ID of this version: #1009315/4
Text MD5: c782f0d610023ddf8346b308c96e2c9a
Transpilation MD5: 9f2ab688b8d4256badbf7907d4d5a18e
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-03 02:32:24
Source code size: 1203 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 431 / 533
Version history: 3 change(s)
Referenced in: [show references]