Java The Complete Reference, Seventh Edition
such as its synchronizers, callable threads, and executors, that are applicable to a wide variety of situations. For these reaso ...
Chapter 26: The Concurrency Utilities 789 java.util.concurrent.atomic java.util.concurrent.atomicfacilitates the use of variable ...
790 Part II: The Java Library the resource at any one time. By default, waiting threads are granted a permit in an undefined ord ...
Chapter 26: The Concurrency Utilities 791 public void run() { System.out.println("Starting " + name); try { // First, get a perm ...
// Now, allow a context switch -- if possible. Thread.sleep(10); } } catch (InterruptedException exc) { System.out.println(exc); ...
Without the use of the semaphore, accesses toShared.countby both threads would have occurred simultaneously, and the increments ...
new Thread(this, "Producer").start(); } public void run() { for(int i=0; i < 20; i++) q.put(i); } } class Consumer implements ...
The sequencing ofput( )andget( )calls is handled by two semaphores:semProdand semCon. Beforeput( )can produce a value, it must a ...
try { cdl.await(); } catch (InterruptedException exc) { System.out.println(exc); } System.out.println("Done"); } } class MyThrea ...
that point. To handle such a situation, the concurrent API supplies theCyclicBarrierclass. It enables you to define a synchroniz ...
798 Part II: The Java Library class MyThread implements Runnable { CyclicBarrier cbar; String name; MyThread(CyclicBarrier c, St ...
new MyThread(cb, "X"); new MyThread(cb, "Y"); new MyThread(cb, "Z"); } The following output will be produced. (The precise order ...
Here is an example that demonstratesExchanger. It creates two threads. One thread creates an empty buffer that will receive the ...
Chapter 26: The Concurrency Utilities 801 UseString(Exchanger<String> c) { ex = c; new Thread(this).start(); } public void ...
802 Part II: The Java Library Also defined is the interfaceScheduledExecutorService, which extendsExecutorService to support the ...
Chapter 26: The Concurrency Utilities 803 cdl2.await(); cdl3.await(); cdl4.await(); } catch (InterruptedException exc) { System. ...
804 Part II: The Java Library B: 1 B: 2 B: 3 B: 4 Done As the output shows, even though the thread pool contains only two thread ...
Chapter 26: The Concurrency Utilities 805 The first form waits for the result indefinitely. The second form allows you to specif ...
806 Part II: The Java Library return sum; } } class Hypot implements Callable<Double> { double side1, side2; Hypot(double ...
Chapter 26: The Concurrency Utilities 807 The first three were added by Java SE 6. AlthoughTimeUnitlets you specify any of these ...
«
37
38
39
40
41
42
43
44
45
46
»
Free download pdf