Demonstrates how deadlock can be hidden in a program : Deadlock « Threads « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Threads » DeadlockScreenshots 
Demonstrates how deadlock can be hidden in a program

// : c13:DiningPhilosophers.java
// Demonstrates how deadlock can be hidden in a program.
// {Args: 5 0 deadlock 4}
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

class Chopstick {
  private static int counter = 0;

  private int number = counter++;

  public String toString() {
    return "Chopstick " + number;
  }
}

class Philosopher extends Thread {
  private static Random rand = new Random();

  private static int counter = 0;

  private int number = counter++;

  private Chopstick leftChopstick;

  private Chopstick rightChopstick;

  static int ponder = 0// Package access

  public Philosopher(Chopstick left, Chopstick right) {
    leftChopstick = left;
    rightChopstick = right;
    start();
  }

  public void think() {
    System.out.println(this " thinking");
    if (ponder > 0)
      try {
        sleep(rand.nextInt(ponder));
      catch (InterruptedException e) {
        throw new RuntimeException(e);
      }
  }

  public void eat() {
    synchronized (leftChopstick) {
      System.out.println(this " has " this.leftChopstick
          " Waiting for " this.rightChopstick);
      synchronized (rightChopstick) {
        System.out.println(this " eating");
      }
    }
  }

  public String toString() {
    return "Philosopher " + number;
  }

  public void run() {
    while (true) {
      think();
      eat();
    }
  }
}

public class DiningPhilosophers {
  public static void main(String[] args) {
    if (args.length < 3) {
      System.err.println("usage:\n"
          "java DiningPhilosophers numberOfPhilosophers "
          "ponderFactor deadlock timeout\n"
          "A nonzero ponderFactor will generate a random "
          "sleep time during think().\n"
          "If deadlock is not the string "
          "'deadlock', the program will not deadlock.\n"
          "A nonzero timeout will stop the program after "
          "that number of seconds.");
      System.exit(1);
    }
    Philosopher[] philosopher = new Philosopher[Integer.parseInt(args[0])];
    Philosopher.ponder = Integer.parseInt(args[1]);
    Chopstick left = new Chopstick(), right = new Chopstick(), first = left;
    int i = 0;
    while (i < philosopher.length - 1) {
      philosopher[i++new Philosopher(left, right);
      left = right;
      right = new Chopstick();
    }
    if (args[2].equals("deadlock"))
      philosopher[inew Philosopher(left, first);
    else
      // Swapping values prevents deadlock:
      philosopher[inew Philosopher(first, left);
    // Optionally break out of program:
    if (args.length >= 4) {
      int delay = Integer.parseInt(args[3]);
      if (delay != 0)
        new Timeout(delay * 1000"Timed out");
    }
  }
///:~

class Timeout extends Timer {
  public Timeout(int delay, final String msg) {
    super(true)// Daemon thread
    schedule(new TimerTask() {
      public void run() {
        System.out.println(msg);
        System.exit(0);
      }
    }, delay);
  }
///:~


           
       
Related examples in the same category
1. Another deadlock demo
2. ReentrantLock: test for deadlocksReentrantLock: test for deadlocks
3. Deadlock DetectingDeadlock Detecting
4. Using interrupt() to break out of a blocked thread.Using interrupt() to break out of a blocked thread.
ww__w.___j___a_va2_s__._c_o___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.