fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class DeadLock {
  9. static class Friend {
  10. private final String name;
  11.  
  12. public Friend(String name) {
  13. this.name = name;
  14. }
  15.  
  16. public String getName() {
  17. return this.name;
  18. }
  19.  
  20. public synchronized void bow(Friend bower) {
  21. //My Changes
  22. System.out.format("%s: %s" + " has bowed to me!%n", this.name, bower.getName()); //Line 17
  23. //System.out.println(this.name + ": " + bower.getName() + " has bowed to me!"); //Line 18
  24. bower.bowBack(this);
  25. }
  26.  
  27. public synchronized void bowBack(Friend bower) {
  28. System.out.format("%s: %s" + " has bowed back to me!%n", this.name, bower.getName());
  29. }
  30. }
  31.  
  32. public static void main(String[] args) {
  33. final Friend alphonse = new Friend("Alphonse");
  34. final Friend gaston = new Friend("Gaston");
  35. new Thread(new Runnable() {
  36. @Override
  37. public void run() {
  38. alphonse.bow(gaston);
  39. }
  40. }).start();
  41.  
  42. new Thread(new Runnable() {
  43. @Override
  44. public void run() {
  45. gaston.bow(alphonse);
  46. }
  47. }).start();
  48. }
  49. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Alphonse: Gaston has bowed to me!
Gaston: Alphonse has bowed back to me!
Gaston: Alphonse has bowed to me!
Alphonse: Gaston has bowed back to me!