diff options
Diffstat (limited to 'test/050-sync-test/src/Main.java')
-rw-r--r-- | test/050-sync-test/src/Main.java | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/test/050-sync-test/src/Main.java b/test/050-sync-test/src/Main.java index 0de3159d9f..ec6d732d5a 100644 --- a/test/050-sync-test/src/Main.java +++ b/test/050-sync-test/src/Main.java @@ -37,8 +37,7 @@ public class Main { System.out.println("GOING"); try { Thread.sleep(1000); - } - catch (InterruptedException ie) { + } catch (InterruptedException ie) { System.out.println("INTERRUPT!"); ie.printStackTrace(); } @@ -52,23 +51,22 @@ public class Main { two = new CpuThread(2); one.start(); - two.start(); try { Thread.sleep(100); - } - catch (InterruptedException ie) { + } catch (InterruptedException ie) { System.out.println("INTERRUPT!"); ie.printStackTrace(); } + two.start(); + //System.out.println("main: off and running"); try { one.join(); two.join(); - } - catch (InterruptedException ie) { + } catch (InterruptedException ie) { System.out.println("INTERRUPT!"); ie.printStackTrace(); } @@ -102,34 +100,29 @@ class CpuThread extends Thread { //System.out.print("thread running -- "); //System.out.println(Thread.currentThread().getName()); - for (int i = 0; i < 10; i++) { - output(mNumber); - } + synchronized (mSyncable) { + for (int i = 0; i < 10; i++) { + output(mNumber); + } - System.out.print("Final result: "); - System.out.println(mCount); + System.out.print("Final result: "); + System.out.println(mCount); + } } void output(int num) { - /* - * Delete the next line; last "final result" should != 20. - */ - synchronized (mSyncable) - { - int i, count; - - count = mCount; + int count = mCount; - System.out.print("going: "); - System.out.println(num); + System.out.print("going: "); + System.out.println(num); - /* burn CPU; adjust end value so we exceed scheduler quantum */ - for (int j = 0; j < 5000; j++) - ; - - count++; - mCount = count; + /* burn CPU; adjust end value so we exceed scheduler quantum */ + for (int j = 0; j < 5000; j++) { + ; } + + count++; + mCount = count; } } @@ -164,14 +157,12 @@ class SleepyThread extends Thread { synchronized (mWaitOnMe) { mWaitOnMe.wait(9000); } - } - catch (InterruptedException ie) { + } catch (InterruptedException ie) { // Expecting this; interrupted should be false. System.out.println(Thread.currentThread().getName() + " interrupted, flag=" + Thread.interrupted()); intr = true; - } - catch (Exception ex) { + } catch (Exception ex) { ex.printStackTrace(); } @@ -180,8 +171,7 @@ class SleepyThread extends Thread { } else { try { Thread.sleep(2000); - } - catch (InterruptedException ie) { + } catch (InterruptedException ie) { System.out.println("PESKY INTERRUPTED?"); } |