You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NestedSyncWithResult.java 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import org.aspectj.testing.Tester;
  2. /** @testcase PR#601 VerifyError if nested sync returning result */
  3. public class NestedSyncWithResult {
  4. public static boolean holdA;
  5. public static boolean aWaiterDone;
  6. public static boolean aHolderDone;
  7. public static void main(String[] args) {
  8. int result = Bug.bug();
  9. Tester.check(0 == result, "0 == result");
  10. checkSynchronization();
  11. Tester.checkAllEvents();
  12. }
  13. public static void checkSynchronization() {
  14. final StringBuffer sb = new StringBuffer();
  15. Tester.expectEvent("holding A; releasing A; invoked bug; ");
  16. final boolean[] holdAstarted = new boolean[] { false };
  17. holdA = true;
  18. Runnable aHolder = new Runnable() {
  19. public void run() {
  20. boolean wroteWait = false;
  21. synchronized (Bug.lockB) {
  22. holdAstarted[0] = true;
  23. while (holdA) {
  24. if (!wroteWait) {
  25. wroteWait = true;
  26. sb.append("holding A; ");
  27. }
  28. sleep();
  29. }
  30. sb.append("releasing A; ");
  31. }
  32. aHolderDone = true;
  33. }
  34. };
  35. Runnable aWaiter = new Runnable() {
  36. public void run() {
  37. while (!holdAstarted[0]) {
  38. sleep();
  39. }
  40. Bug.bug();
  41. sb.append("invoked bug; ");
  42. aWaiterDone = true;
  43. }
  44. };
  45. new Thread(aHolder).start();
  46. new Thread(aWaiter).start();
  47. sleep();
  48. holdA = false;
  49. while (!aWaiterDone && !aHolderDone) {
  50. sleep();
  51. }
  52. Tester.event(sb.toString());
  53. //System.err.println("got: " + sb.toString());
  54. }
  55. public static void sleep() {
  56. try {
  57. Thread.currentThread().sleep(300);
  58. } catch (InterruptedException e) {
  59. } // ignore
  60. }
  61. }
  62. class Bug {
  63. public static Object lockA = new Object();
  64. public static Object lockB = new Object();
  65. static int bug() {
  66. synchronized (lockA) {
  67. synchronized (lockB) {
  68. return 0;
  69. }
  70. }
  71. }
  72. }