Cleanup and fixes
This commit is contained in:
parent
45d4e5d85b
commit
a2a7b53b95
@ -16,26 +16,26 @@ public class Atomicity {
|
||||
public Atomicity();
|
||||
Code:
|
||||
0: aload_0
|
||||
1: invokespecial #1 // Method
|
||||
1: invokespecial #1 // Method
|
||||
java/lang/Object."<init>":()V
|
||||
4: return
|
||||
void f1();
|
||||
Code:
|
||||
0: aload_0
|
||||
1: dup
|
||||
2: getfield #2 // Field i:I
|
||||
2: getfield #2 // Field i:I
|
||||
5: iconst_1
|
||||
6: iadd
|
||||
7: putfield #2 // Field i:I
|
||||
7: putfield #2 // Field i:I
|
||||
10: return
|
||||
void f2();
|
||||
Code:
|
||||
0: aload_0
|
||||
1: dup
|
||||
2: getfield #2 // Field i:I
|
||||
2: getfield #2 // Field i:I
|
||||
5: iconst_3
|
||||
6: iadd
|
||||
7: putfield #2 // Field i:I
|
||||
7: putfield #2 // Field i:I
|
||||
10: return
|
||||
}
|
||||
*/
|
||||
|
@ -8,7 +8,9 @@ import onjava.TimedAbort;
|
||||
public class AtomicityTest implements Runnable {
|
||||
private int i = 0;
|
||||
public int getValue() { return i; }
|
||||
private synchronized void evenIncrement() { i++; i++; }
|
||||
private synchronized void evenIncrement() {
|
||||
i++; i++;
|
||||
}
|
||||
@Override
|
||||
public void run() {
|
||||
while(true)
|
||||
@ -16,7 +18,8 @@ public class AtomicityTest implements Runnable {
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
new TimedAbort(4);
|
||||
ExecutorService es = Executors.newCachedThreadPool();
|
||||
ExecutorService es =
|
||||
Executors.newCachedThreadPool();
|
||||
AtomicityTest at = new AtomicityTest();
|
||||
es.execute(at);
|
||||
while(true) {
|
||||
|
@ -6,8 +6,8 @@
|
||||
// Synchronizing blocks instead of entire methods. Also
|
||||
// demonstrates protection of a non-thread-safe class
|
||||
// with a thread-safe one.
|
||||
// {java threads.CriticalSection}
|
||||
package threads;
|
||||
// {java lowlevel.CriticalSection}
|
||||
package lowlevel;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.*;
|
||||
|
@ -4,8 +4,8 @@
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
// {ThrowsException} on a multiprocessor machine
|
||||
// Using explicit Lock objects to create critical sections
|
||||
// {java threads.ExplicitCriticalSection}
|
||||
package threads;
|
||||
// {java lowlevel.ExplicitCriticalSection}
|
||||
package lowlevel;
|
||||
import java.util.concurrent.locks.*;
|
||||
|
||||
// Synchronize the entire method:
|
||||
|
@ -1,16 +0,0 @@
|
||||
// lowlevel/QuittableTask.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import onjava.Nap;
|
||||
|
||||
public class QuittableTask implements Runnable {
|
||||
private boolean running = true;
|
||||
public void quit() { running = false; }
|
||||
public boolean running() { return running; }
|
||||
@Override
|
||||
public void run() {
|
||||
while(running)
|
||||
new Nap(100);
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
// lowlevel/QuittingTasks.java
|
||||
// (c)2017 MindView LLC: see Copyright.txt
|
||||
// We make no guarantees that this code is fit for any purpose.
|
||||
// Visit http://OnJava8.com for more book information.
|
||||
import java.util.*;
|
||||
import java.util.stream.*;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class QuittingTasks {
|
||||
public static void main(String[] args)
|
||||
throws InterruptedException {
|
||||
ExecutorService exec =
|
||||
Executors.newCachedThreadPool();
|
||||
List<QuittableTask> tasks =
|
||||
IntStream.range(1, 1000)
|
||||
.mapToObj(i -> new QuittableTask())
|
||||
.peek(exec::execute)
|
||||
.collect(Collectors.toList());
|
||||
TimeUnit.MILLISECONDS.sleep(100);
|
||||
tasks.forEach(QuittableTask::quit);
|
||||
exec.shutdown();
|
||||
exec.awaitTermination(1, TimeUnit.SECONDS);
|
||||
// See if any tasks are still running:
|
||||
System.out.println(
|
||||
tasks.stream()
|
||||
.anyMatch(QuittableTask::running));
|
||||
}
|
||||
}
|
||||
/* Output:
|
||||
false
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user