File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66// to give up on trying to acquire a lock
77import java .util .concurrent .*;
88import java .util .concurrent .locks .*;
9+ import onjava .Nap ;
910
1011public class AttemptLocking {
1112 private ReentrantLock lock = new ReentrantLock ();
@@ -46,7 +47,7 @@ public void run() {
4647 System .out .println ("acquired" );
4748 }
4849 }.start ();
49- Thread . yield (); // Give the 2nd task a chance
50+ new Nap ( 10 ); // Give the 2nd task a chance
5051 al .untimed (); // False -- lock grabbed by task
5152 al .timed (); // False -- lock grabbed by task
5253 }
Original file line number Diff line number Diff line change 22// (c)2017 MindView LLC: see Copyright.txt
33// We make no guarantees that this code is fit for any purpose.
44// Visit http://OnJava8.com for more book information.
5- // (Behavior may have changed in Java 8)
65// Synchronizing blocks instead of entire methods. Also
76// demonstrates protection of a non-thread-safe class
87// with a thread-safe one.
9- // {java lowlevel.CriticalSection}
10- package lowlevel ;
118import java .util .concurrent .*;
129import java .util .concurrent .atomic .*;
1310import java .util .*;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 55// Preventing thread collisions with mutexes
66// {IgnoreOutput} // No output validation
77import java .util .concurrent .locks .*;
8+ import onjava .Nap ;
89
910public class MutexEvenProducer extends IntGenerator {
1011 private int currentEvenValue = 0 ;
@@ -14,7 +15,7 @@ public int next() {
1415 lock .lock ();
1516 try {
1617 ++currentEvenValue ;
17- Thread . yield ( ); // Cause failure faster
18+ new Nap ( 10 ); // Cause failure faster
1819 ++currentEvenValue ;
1920 return currentEvenValue ;
2021 } finally {
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ public void run() {
7373 // Fill it up fast with random priorities:
7474 for (int i = 0 ; i < 20 ; i ++) {
7575 queue .add (new PrioritizedTask (rand .nextInt (10 )));
76- Thread . yield ( );
76+ new Nap ( 10 );
7777 }
7878 // Trickle in highest-priority jobs:
7979 for (int i = 0 ; i < 10 ; i ++) {
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://OnJava8.com for more book information.
55// Synchronizing on another object
6+ import onjava .Nap ;
67
78class DualSynch {
89 private Object syncObject = new Object ();
910 public synchronized void f () {
1011 for (int i = 0 ; i < 5 ; i ++) {
1112 System .out .println ("f()" );
12- Thread . yield ( );
13+ new Nap ( 10 );
1314 }
1415 }
1516 public void g () {
1617 synchronized (syncObject ) {
1718 for (int i = 0 ; i < 5 ; i ++) {
1819 System .out .println ("g()" );
19- Thread . yield ( );
20+ new Nap ( 10 );
2021 }
2122 }
2223 }
You can’t perform that action at this time.
0 commit comments