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

!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.lock();
    try {
      print("Thread A has lock A.");
      sleep1Second();
      print("Thread A getting lock B.");
      b.lock();
      try {
        print("Thread A successful!");
      } finally {
        b.unlock();
      }
    } finally {
      a.unlock();
    }
  }
  
  thread "B" {
    b.lock();
    try {
      print("Thread B has lock B.");
      sleep1Second();
      print("Thread B getting lock A.");
      a.lock();
      try {
        print("Thread B successful!");
      } finally {
        a.unlock();
      }
    } finally {
      b.unlock();
    }
  }
 
  sleepQuietly();
}

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: 419 / 461
Version history: 3 change(s)
Referenced in: [show references]