This commit is contained in:
Bruce Eckel 2015-06-03 11:41:10 -07:00
parent f1f6c79e2e
commit ded2d52c66
33 changed files with 102 additions and 79 deletions

View File

@ -32,7 +32,7 @@
[ ] Appendix: Passing & Returning Objects
[X] Appendix: Network Programming
[X] Appendix: Remote Methods
[ ] Appendix: Logging
[X] Appendix: Logging
[X] Appendix: Debugging
[X] Appendix: Profiling and Optimizing
[ ] Appendix: The Benefits and Costs of Static Type Checking

View File

@ -64,11 +64,10 @@ ENHANCEMENTS, OR MODIFICATIONS.
Please note that MindView LLC maintains a Web site which
is the sole distribution point for electronic copies of the
Source Code, http://www.MindView.net (and official mirror
sites), where it is freely available under the terms stated
above.
Source Code, https://github.com/BruceEckel/TIJ-Directors-Cut/,
where it is freely available under the terms stated above.
If you think you've found an error in the Source Code,
please submit a correction using the feedback system that
you will find at http://www.MindView.net.
please submit a correction at:
https://github.com/BruceEckel/TIJ-Directors-Cut/issues
///:~

View File

@ -1,6 +1,7 @@
//: concurrency/CarBuilder.java
// ©2015 MindView LLC: see Copyright.txt
// A complex example of tasks working together.
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -209,4 +210,4 @@ public class CarBuilder {
TimeUnit.SECONDS.sleep(7);
exec.shutdownNow();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,5 +1,6 @@
//: concurrency/CountDownLatchDemo.java
// ©2015 MindView LLC: see Copyright.txt
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -68,4 +69,4 @@ public class CountDownLatchDemo {
print("Launched all tasks");
exec.shutdown(); // Quit when all tasks complete
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -25,4 +25,4 @@ public class DaemonFromFactory implements Runnable {
print("All daemons started");
TimeUnit.MILLISECONDS.sleep(500); // Run for a while
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -2,6 +2,7 @@
// ©2015 MindView LLC: see Copyright.txt
// Demonstrates how deadlock can be hidden in a program.
// {Args: 0 5 timeout}
// {FirstAndLast: 10 Lines}
import java.util.concurrent.*;
public class DeadlockingDiningPhilosophers {
@ -27,4 +28,4 @@ public class DeadlockingDiningPhilosophers {
}
exec.shutdownNow();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,5 +1,6 @@
//: concurrency/FastSimulation.java
// ©2015 MindView LLC: see Copyright.txt
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;
@ -49,4 +50,4 @@ public class FastSimulation {
TimeUnit.SECONDS.sleep(5);
exec.shutdownNow();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -2,6 +2,7 @@
// ©2015 MindView LLC: see Copyright.txt
// Dining philosophers without deadlock.
// {Args: 5 5 timeout}
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
public class FixedDiningPhilosophers {
@ -31,4 +32,4 @@ public class FixedDiningPhilosophers {
}
exec.shutdownNow();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -3,6 +3,7 @@
// Rewriting innerclasses/GreenhouseController.java
// to use a ScheduledThreadPoolExecutor.
// {Args: 5000}
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.*;
@ -163,4 +164,4 @@ public class GreenhouseScheduler {
gh.repeat(gh.new ThermostatDay(), 0, 1400);
gh.repeat(gh.new CollectData(), 500, 500);
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,7 +1,7 @@
//: concurrency/HorseRace.java
// ©2015 MindView LLC: see Copyright.txt
// Using CyclicBarriers.
// {ValidateByHand}
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -86,4 +86,4 @@ public class HorseRace {
}
new HorseRace(nHorses, pause);
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,5 +1,6 @@
//: concurrency/PriorityBlockingQueueDemo.java
// ©2015 MindView LLC: see Copyright.txt
// {OutputFirstAndLast: 12 Lines}
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -121,4 +122,4 @@ public class PriorityBlockingQueueDemo {
exec.execute(new PrioritizedTaskProducer(queue, exec));
exec.execute(new PrioritizedTaskConsumer(queue));
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,5 +1,6 @@
//: concurrency/ReaderWriterList.java
// ©2015 MindView LLC: see Copyright.txt
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.concurrent.locks.*;
import java.util.*;
@ -83,4 +84,4 @@ class ReaderWriterListTest {
for(int i = 0; i < writers; i++)
exec.execute(new Writer());
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,6 +1,7 @@
//: concurrency/SemaphoreDemo.java
// ©2015 MindView LLC: see Copyright.txt
// Testing the Pool class
// {OutputFirstAndLast: 10 Lines}
import java.util.concurrent.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -65,4 +66,4 @@ public class SemaphoreDemo {
pool.checkIn(f); // Second checkIn ignored
exec.shutdown();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -161,4 +161,4 @@ public class ThreadVariations {
new InnerRunnable2("InnerRunnable2");
new ThreadMethod("ThreadMethod").runTask();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -136,4 +136,4 @@ public class ToastOMatic {
TimeUnit.SECONDS.sleep(5);
exec.shutdownNow();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -33,6 +33,7 @@
<jrun cls="FixedDiningPhilosophers" arguments="5 5 timeout" />
<jrun cls="FixedThreadPool" />
<jrun cls="GreenhouseScheduler" arguments="5000" />
<jrun cls="HorseRace" />
<jrun cls="Interrupting" />
<jrun cls="Interrupting2" />
<jrun cls="InterruptingIdiom" arguments="1100" />

View File

@ -1,6 +1,7 @@
//: containers/CanonicalMapping.java
// ©2015 MindView LLC: see Copyright.txt
// Demonstrates WeakHashMap.
// {NoOutput}
import java.util.*;
class Element {
@ -47,4 +48,4 @@ public class CanonicalMapping {
}
System.gc();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,6 +1,7 @@
//: containers/Lists.java
// ©2015 MindView LLC: see Copyright.txt
// Things you can do with Lists.
// {OutputFirstAndLast: 10 Lines}
import java.util.*;
import net.mindview.util.*;
import static net.mindview.util.Print.*;
@ -123,4 +124,4 @@ public class Lists {
new LinkedList<>(Countries.names(25)));
testLinkedList();
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,6 +1,7 @@
//: containers/References.java
// ©2015 MindView LLC: see Copyright.txt
// Demonstrates Reference objects
// {OutputFirstAndLast: 10 Lines}
import java.lang.ref.*;
import java.util.*;
@ -60,4 +61,4 @@ public class References {
checkQueue();
}
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,6 +1,7 @@
//: control/WhileTest.java
// ©2015 MindView LLC: see Copyright.txt
// Demonstrates the while loop.
// {OutputFirstAndLast: 5 Lines}
public class WhileTest {
static boolean condition() {
@ -13,4 +14,4 @@ public class WhileTest {
System.out.println("Inside 'while'");
System.out.println("Exited 'while'");
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -28,4 +28,4 @@ public enum ConstantSpecificMethod {
for(ConstantSpecificMethod csm : values())
System.out.println(csm.getInfo());
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -1,6 +1,7 @@
//: generics/Apply.java
// ©2015 MindView LLC: see Copyright.txt
// {main: ApplyTest}
// {OutputFirstAndLast: 10 Lines}
import java.lang.reflect.*;
import java.util.*;
import static net.mindview.util.Print.*;
@ -69,4 +70,4 @@ class ApplyTest {
}
Apply.apply(shapeQ, Shape.class.getMethod("rotate"));
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -4,11 +4,13 @@
// {Args: motif}
import javax.swing.*;
import java.awt.*;
import java.util.*;
import static net.mindview.util.SwingConsole.*;
public class LookAndFeel extends JFrame {
private String[] choices =
"Eeny Meeny Minnie Mickey Moe Larry Curly".split(" ");
"Eeny Meeny Minnie Mickey Moe Larry Curly"
.split(" ");
private Component[] samples = {
new JButton("JButton"),
new JTextField("JTextField"),
@ -18,55 +20,66 @@ public class LookAndFeel extends JFrame {
new JComboBox<>(choices),
new JList<>(choices),
};
private static
Map<String,String> optionMap = new HashMap<>();
private static String options = "";
public LookAndFeel() {
super("Look And Feel");
setLayout(new FlowLayout());
for(Component component : samples)
add(component);
}
public static void initOptions() {
if(optionMap.isEmpty()) {
UIManager.LookAndFeelInfo[] lafi =
UIManager.getInstalledLookAndFeels();
for(UIManager.LookAndFeelInfo lf : lafi) {
String classname = lf.getClassName();
String[] parts = classname.split("\\.");
String option = parts[parts.length - 2];
optionMap.put(option, classname);
}
for(String option : optionMap.keySet())
options += option + " | ";
options =
options.substring(0, options.length() - 3);
options = "cross | system | " + options;
}
}
private static void usageError() {
System.out.println(
"Usage:LookAndFeel [cross|system|motif]");
"Usage:LookAndFeel [ " + options + " ]");
System.exit(1);
}
public static void main(String[] args) {
initOptions();
if(args.length == 0) usageError();
switch(args[0]) {
case "cross":
try {
try {
switch(args[0]) {
case "cross":
UIManager.setLookAndFeel(UIManager.
getCrossPlatformLookAndFeelClassName());
} catch(ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
break;
break;
case "system":
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch(ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
break;
break;
case "motif":
try {
UIManager.setLookAndFeel("com.sun.java."+
"swing.plaf.motif.MotifLookAndFeel");
} catch(ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
UIManager.setLookAndFeel("com.sun.java."+
"swing.plaf.motif.MotifLookAndFeel");
break;
default:
usageError();
if(optionMap.containsKey(args[0]))
UIManager.setLookAndFeel(
optionMap.get(args[0]));
else
usageError();
}
} catch(ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
usageError();
}
// Note the look & feel must be set before
// any components are created.

View File

@ -1,6 +1,6 @@
//: holding/ApplesAndOrangesWithoutGenerics.java
// ©2015 MindView LLC: see Copyright.txt
// Simple container example (produces compiler warnings).
// Simple container use (suppressing compiler warnings)
// {ThrowsException}
import java.util.*;
@ -25,4 +25,4 @@ public class ApplesAndOrangesWithoutGenerics {
// Orange is detected only at run time
}
}
} /* (Execute to see output) *///:~
} ///:~

View File

@ -7,9 +7,9 @@ public class BufferedInputFile {
public static String
read(String filename) throws IOException {
StringBuilder sb;
try ( // Reading input by lines:
BufferedReader in = new BufferedReader(
new FileReader(filename))) {
// Reading input by lines:
try(BufferedReader in = new BufferedReader(
new FileReader(filename))) {
String s;
sb = new StringBuilder();
while((s = in.readLine())!= null)

View File

@ -7,10 +7,10 @@ public class FileOutputShortcut {
public static void main(String[] args)
throws IOException {
BufferedReader in = new BufferedReader(
new StringReader(
BufferedInputFile.read("FileOutputShortcut.java")));
try ( // Here's the shortcut:
PrintWriter out = new PrintWriter(file)) {
new StringReader(BufferedInputFile.read(
"FileOutputShortcut.java")));
// Here's the shortcut:
try(PrintWriter out = new PrintWriter(file)) {
int lineCount = 1;
String s;
while((s = in.readLine()) != null )

View File

@ -28,8 +28,7 @@ public class ChatterClient extends Thread {
System.exit(1);
} catch(SocketException e) {
System.err.println("Can't open socket");
e.printStackTrace();
System.exit(1);
throw new RuntimeException(e);
}
System.out.println("ChatterClient starting");
}
@ -49,8 +48,7 @@ public class ChatterClient extends Thread {
Dgram.toString(dp);
System.out.println(rcvd);
} catch(IOException e) {
e.printStackTrace();
System.exit(1);
throw new RuntimeException(e);
}
}
@Override

View File

@ -40,7 +40,7 @@ public class ChatterServer {
System.exit(1);
} catch(IOException e) {
System.out.println("Communication error");
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static void main(String[] args) {

View File

@ -64,8 +64,7 @@ public class ShapeFactory2 {
ShapeFactory.createShape(shlist1));
}
} catch(BadShapeCreation e) {
e.printStackTrace();
return;
throw new RuntimeException(e);
}
shapes.forEach(Shape::draw);
shapes.forEach(Shape::erase);

View File

@ -51,8 +51,7 @@ public class ShapeFactory1 {
shapes.add(Shape.factory(shlist1));
}
} catch(BadShapeCreation e) {
e.printStackTrace();
return;
throw new RuntimeException(e);
}
shapes.forEach(Shape::draw);
shapes.forEach(Shape::erase);

View File

@ -29,7 +29,7 @@ public class ParseTrash {
NumberFormatException |
Trash.PrototypeNotFoundException |
Trash.CannotCreateTrashException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
// Special case to handle ArrayList:

View File

@ -16,7 +16,7 @@ class DepthReading implements Cloneable {
try {
o = super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return o;
}
@ -43,7 +43,7 @@ class TemperatureReading implements Cloneable {
try {
o = super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return o;
}
@ -73,7 +73,7 @@ class OceanReading implements Cloneable {
try {
o = (OceanReading)super.clone();
} catch(CloneNotSupportedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
// Must clone references:
o.depth = (DepthReading)o.depth.clone();

View File

@ -6,4 +6,4 @@ public class PetCount2 {
public static void main(String[] args) {
PetCount.countPets(Pets.creator);
}
} /* (Execute to see output) *///:~
} ///:~