Results 1 to 1 of 1
Thread: How to create a simple deadlock
-
How to create a simple deadlock
This Java tip shows how to create a simple deadlock.
Java Code:public class AnotherDeadLock { public static void main(String[] args) { final Object resource1 = "resource1"; final Object resource2 = "resource2"; // t1 tries to lock resource1 then resource2 Thread t1 = new Thread() { public void run() { // Lock resource 1 synchronized (resource1) { System.out.println("Thread 1: locked resource 1"); try { Thread.sleep(50); } catch (InterruptedException e) { } synchronized (resource2) { System.out.println("Thread 1: locked resource 2"); } } } }; // t2 tries to lock resource2 then resource1 Thread t2 = new Thread() { public void run() { synchronized (resource2) { System.out.println("Thread 2: locked resource 2"); try { Thread.sleep(50); } catch (InterruptedException e) { } synchronized (resource1) { System.out.println("Thread 2: locked resource 1"); } } } }; // If all goes as planned, deadlock will occur, // and the program will never exit. t1.start(); t2.start(); } }
Similar Threads
-
simple GUI
By dim_ath in forum New To JavaReplies: 3Last Post: 01-07-2008, 03:00 PM -
A simple DOM reader
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:24 AM -
Probably a really simple question...
By ibanez270dx in forum New To JavaReplies: 0Last Post: 11-16-2007, 01:27 AM -
Deadlock detection tools documentation
By goldhouse in forum Threads and SynchronizationReplies: 0Last Post: 07-18-2007, 05:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks