File tree Expand file tree Collapse file tree 10 files changed +19
-9
lines changed
Expand file tree Collapse file tree 10 files changed +19
-9
lines changed Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// Cleanup and inheritance
6+ // {main: polymorphism.Frog}
67package polymorphism ;
78
89class Characteristic {
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// How to read from standard input
6- // {TimeOutDuringTesting}
76import java .io .*;
7+ import onjava .TimedAbort ;
88
99public class Echo {
1010 public static void
1111 main (String [] args ) throws IOException {
12+ new TimedAbort (4 );
1213 BufferedReader stdin = new BufferedReader (
1314 new InputStreamReader (System .in ));
1415 String s ;
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// Atomic classes are occasionally useful in regular code
6- // {TimeOutDuringTesting}
76// {IgnoreOutput} // No output validation
87import java .util .concurrent .atomic .*;
8+ import onjava .TimedAbort ;
99
1010public class AtomicEvenSupplier extends IntSupplier {
1111 private AtomicInteger currentEvenValue =
@@ -15,6 +15,7 @@ public int next() {
1515 return currentEvenValue .addAndGet (2 );
1616 }
1717 public static void main (String [] args ) {
18+ new TimedAbort (4 );
1819 EvenChecker .test (new AtomicEvenSupplier ());
1920 }
2021}
Original file line number Diff line number Diff line change 22// (c)2016 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5- // {TimeOutDuringTesting}
65import java .util .concurrent .*;
6+ import onjava .TimedAbort ;
77
88public class AtomicityTest implements Runnable {
99 private int i = 0 ;
@@ -15,6 +15,7 @@ public void run() {
1515 evenIncrement ();
1616 }
1717 public static void main (String [] args ) {
18+ new TimedAbort (4 );
1819 ExecutorService es = Executors .newCachedThreadPool ();
1920 AtomicityTest at = new AtomicityTest ();
2021 es .execute (at );
Original file line number Diff line number Diff line change 22// (c)2016 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5- // {TimeOutDuringTesting}
65import java .util .concurrent .*;
6+ import onjava .TimedAbort ;
77
88class ExceptionThread2 implements Runnable {
99 @ Override
@@ -40,6 +40,7 @@ public Thread newThread(Runnable r) {
4040
4141public class CaptureUncaughtException {
4242 public static void main (String [] args ) {
43+ new TimedAbort (4 );
4344 ExecutorService exec = Executors .newCachedThreadPool (
4445 new HandlerThreadFactory ());
4546 exec .execute (new ExceptionThread2 ());
Original file line number Diff line number Diff line change 22// (c)2016 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5- // {TimeOutDuringTesting}
65// (Behavior may have changed in Java 8)
76// Synchronizing blocks instead of entire methods. Also
87// demonstrates protection of a non-thread-safe class
1110import java .util .concurrent .*;
1211import java .util .concurrent .atomic .*;
1312import java .util .*;
13+ import onjava .TimedAbort ;
1414
1515class Pair { // Not thread-safe
1616 private int x , y ;
@@ -139,6 +139,7 @@ public class CriticalSection {
139139 System .exit (0 );
140140 }
141141 public static void main (String [] args ) {
142+ new TimedAbort (4 );
142143 PairManager
143144 pman1 = new PairManager1 (),
144145 pman2 = new PairManager2 ();
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// When threads collide
6- // {TimeOutDuringTesting}
6+ import onjava . TimedAbort ;
77
88public class EvenSupplier extends IntSupplier {
99 private int currentEvenValue = 0 ;
@@ -14,6 +14,7 @@ public int next() {
1414 return currentEvenValue ;
1515 }
1616 public static void main (String [] args ) {
17+ new TimedAbort (4 );
1718 EvenChecker .test (new EvenSupplier ());
1819 }
1920}
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// Preventing thread collisions with mutexes
6- // {TimeOutDuringTesting}
76// {IgnoreOutput} // No output validation
87import java .util .concurrent .locks .*;
8+ import onjava .TimedAbort ;
99
1010public class MutexEvenSupplier extends IntSupplier {
1111 private int currentEvenValue = 0 ;
@@ -23,6 +23,7 @@ public int next() {
2323 }
2424 }
2525 public static void main (String [] args ) {
26+ new TimedAbort (4 );
2627 EvenChecker .test (new MutexEvenSupplier ());
2728 }
2829}
Original file line number Diff line number Diff line change 22// (c)2016 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5- // {TimeOutDuringTesting}
65import java .util .concurrent .*;
6+ import onjava .TimedAbort ;
77
88public class SettingDefaultHandler {
99 public static void main (String [] args ) {
10+ new TimedAbort (4 );
1011 Thread .setDefaultUncaughtExceptionHandler (
1112 new MyUncaughtExceptionHandler ());
1213 ExecutorService es = Executors .newCachedThreadPool ();
Original file line number Diff line number Diff line change 33// We make no guarantees that this code is fit for any purpose.
44// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55// Simplifying mutexes with the synchronized keyword
6- // {TimeOutDuringTesting}
76// {IgnoreOutput} // No output validation
7+ import onjava .TimedAbort ;
88
99public class
1010SynchronizedEvenSupplier extends IntSupplier {
@@ -17,6 +17,7 @@ public synchronized int next() {
1717 return currentEvenValue ;
1818 }
1919 public static void main (String [] args ) {
20+ new TimedAbort (4 );
2021 EvenChecker .test (new SynchronizedEvenSupplier ());
2122 }
2223}
You can’t perform that action at this time.
0 commit comments