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

48
LINES

< > BotCompany Repo | #1009312 // Java DeadLock Timeout Test with ReentrantLock [WORKS, but usually both threads fail]

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

Libraryless. Click here for Pure Java version (1536L/11K/39K).

1  
!7
2  
3  
static Lock a = fairLock();
4  
static Lock b = fairLock();
5  
static int timeout = 10000;
6  
7  
p-tt {
8  
  centerHigherConsole();
9  
  startDeadlockDetector_noStop = true;
10  
  startDeadlockDetector();
11  
  print("Initiating deadlock with timeout " + timeout + " ms... :-)\n");
12  
  
13  
  thread "A" {
14  
    lockOrFail(a, timeout);
15  
    try {
16  
      print("Thread A has lock A.");
17  
      sleep1Second();
18  
      print("Thread A getting lock B.");
19  
      lockOrFail(b, timeout);
20  
      try {
21  
        print("Thread A successful!");
22  
      } finally {
23  
        b.unlock();
24  
      }
25  
    } finally {
26  
      a.unlock();
27  
    }
28  
  }
29  
  
30  
  thread "B" {
31  
    lockOrFail(b, timeout);
32  
    try {
33  
      print("Thread B has lock B.");
34  
      sleep1Second();
35  
      print("Thread B getting lock A.");
36  
      lockOrFail(a, timeout);
37  
      try {
38  
        print("Thread B successful!");
39  
      } finally {
40  
        a.unlock();
41  
      }
42  
    } finally {
43  
      b.unlock();
44  
    }
45  
 }
46  
  
47  
  sleepQuietly();
48  
}

Author comment

Began life as a copy of #1009311

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: #1009312
Snippet name: Java DeadLock Timeout Test with ReentrantLock [WORKS, but usually both threads fail]
Eternal ID of this version: #1009312/5
Text MD5: 1c7118608cd9e767cfe42d075d5a322a
Transpilation MD5: d2f06cfde8db86b887eaf6c423aaedc4
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:21:35
Source code size: 982 bytes / 48 lines
Pitched / IR pitched: No / No
Views / Downloads: 500 / 557
Version history: 4 change(s)
Referenced in: [show references]