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).

!7

static Lock a = fairLock();
static Lock b = fairLock();

p-tt {
  centerHigherConsole();
  
  doEvery_daemon(1000, r {
    L<ThreadInfo> lockingThreads = printDeadlocks();
    if (nempty(lockingThreads)) {
      print("Interrupting locking threads.");
      L<Thread> threads = threadInfosToThreads(lockingThreads);
      print("Interrupting " + n(threads, "thread"));
      for (Thread t : threads) t.interrupt();
    }
  });
  
  print("Initiating deadlock... :-)\n");
  
thread "A" {
    a.lockInterruptibly();
    try {
      print("Thread A has lock A.");
      sleep1Second();
      print("Thread A getting lock B.");
      b.lockInterruptibly();
      try {
        print("Thread A successful!");
      } finally {
        b.unlock();
      }
    } finally {
      a.unlock();
    }
  }
  
  thread "B" {
    b.lockInterruptibly();
    try {
      print("Thread B has lock B.");
      sleep1Second();
      print("Thread B getting lock A.");
      a.lockInterruptibly();
      try {
        print("Thread B successful!");
      } finally {
        a.unlock();
      }
    } finally {
      b.unlock();
    }
  }
 
  sleepQuietly();
}

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: 420 / 454
Version history: 2 change(s)
Referenced in: [show references]