All book examples resolved and incorporated
This commit is contained in:
parent
7265b6aceb
commit
ebd816f37e
@ -100,7 +100,7 @@ class TellerManager implements Runnable {
|
|||||||
private Queue<Teller> tellersDoingOtherThings =
|
private Queue<Teller> tellersDoingOtherThings =
|
||||||
new LinkedList<Teller>();
|
new LinkedList<Teller>();
|
||||||
private int adjustmentPeriod;
|
private int adjustmentPeriod;
|
||||||
private static Random rand = new Random(47);
|
|
||||||
public TellerManager(ExecutorService e,
|
public TellerManager(ExecutorService e,
|
||||||
CustomerLine customers, int adjustmentPeriod) {
|
CustomerLine customers, int adjustmentPeriod) {
|
||||||
exec = e;
|
exec = e;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
//: concurrency/ExplicitCriticalSection.java
|
//: concurrency/ExplicitCriticalSection.java
|
||||||
|
// {ThrowsException} on a multiprocessor machine
|
||||||
// Using explicit Lock objects to create critical sections.
|
// Using explicit Lock objects to create critical sections.
|
||||||
package concurrency;
|
package concurrency;
|
||||||
import java.util.concurrent.locks.*;
|
import java.util.concurrent.locks.*;
|
||||||
@ -6,7 +7,7 @@ import java.util.concurrent.locks.*;
|
|||||||
// Synchronize the entire method:
|
// Synchronize the entire method:
|
||||||
class ExplicitPairManager1 extends PairManager {
|
class ExplicitPairManager1 extends PairManager {
|
||||||
private Lock lock = new ReentrantLock();
|
private Lock lock = new ReentrantLock();
|
||||||
public synchronized void increment() {
|
public void increment() {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
p.incrementX();
|
p.incrementX();
|
||||||
@ -42,7 +43,4 @@ public class ExplicitCriticalSection {
|
|||||||
pman2 = new ExplicitPairManager2();
|
pman2 = new ExplicitPairManager2();
|
||||||
CriticalSection.testApproaches(pman1, pman2);
|
CriticalSection.testApproaches(pman1, pman2);
|
||||||
}
|
}
|
||||||
} /* Output: (Sample)
|
} ///:~
|
||||||
pm1: Pair: x: 15, y: 15 checkCounter = 174035
|
|
||||||
pm2: Pair: x: 16, y: 16 checkCounter = 2608588
|
|
||||||
*///:~
|
|
@ -108,7 +108,6 @@ class PrioritizedTaskConsumer implements Runnable {
|
|||||||
|
|
||||||
public class PriorityBlockingQueueDemo {
|
public class PriorityBlockingQueueDemo {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Random rand = new Random(47);
|
|
||||||
ExecutorService exec = Executors.newCachedThreadPool();
|
ExecutorService exec = Executors.newCachedThreadPool();
|
||||||
PriorityBlockingQueue<Runnable> queue =
|
PriorityBlockingQueue<Runnable> queue =
|
||||||
new PriorityBlockingQueue<Runnable>();
|
new PriorityBlockingQueue<Runnable>();
|
||||||
|
@ -22,7 +22,8 @@ public class MapEntry<K,V> implements Map.Entry<K,V> {
|
|||||||
}
|
}
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if(!(o instanceof MapEntry)) return false;
|
if(!(o instanceof MapEntry)) return false;
|
||||||
MapEntry me = (MapEntry)o;
|
@SuppressWarnings("unchecked")
|
||||||
|
MapEntry<K,V> me = (MapEntry<K,V>)o;
|
||||||
return
|
return
|
||||||
(key == null ?
|
(key == null ?
|
||||||
me.getKey() == null : key.equals(me.getKey())) &&
|
me.getKey() == null : key.equals(me.getKey())) &&
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//: generics/UseList.java
|
//: generics/UseList.java
|
||||||
// {CompileTimeError} (Won't compile)
|
// {CompileTimeError} (Will not compile)
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class UseList<W,T> {
|
public class UseList<W,T> {
|
||||||
|
@ -7,11 +7,9 @@
|
|||||||
// www.javassist.org }
|
// www.javassist.org }
|
||||||
package net.mindview.atunit;
|
package net.mindview.atunit;
|
||||||
import javassist.*;
|
import javassist.*;
|
||||||
import javassist.expr.*;
|
|
||||||
import javassist.bytecode.*;
|
import javassist.bytecode.*;
|
||||||
import javassist.bytecode.annotation.*;
|
import javassist.bytecode.annotation.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
|
||||||
import net.mindview.util.*;
|
import net.mindview.util.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
package net.mindview.atunit;
|
package net.mindview.atunit;
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
// Both fields and methods may be tagged as properties:
|
// Both fields and methods can be tagged as properties:
|
||||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface TestProperty {} ///:~
|
public @interface TestProperty {} ///:~
|
@ -11,7 +11,7 @@ extends AbstractMap<Integer,String> {
|
|||||||
.split(" ");
|
.split(" ");
|
||||||
public CountingMapData(int size) {
|
public CountingMapData(int size) {
|
||||||
if(size < 0) this.size = 0;
|
if(size < 0) this.size = 0;
|
||||||
this.size = size;
|
else this.size = size;
|
||||||
}
|
}
|
||||||
private static class Entry
|
private static class Entry
|
||||||
implements Map.Entry<Integer,String> {
|
implements Map.Entry<Integer,String> {
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
//: net/mindview/util/Generated.java
|
//: net/mindview/util/Generated.java
|
||||||
package net.mindview.util;
|
package net.mindview.util;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class Generated {
|
public class Generated {
|
||||||
// Fill an existing array:
|
// Fill an existing array:
|
||||||
|
@ -17,7 +17,7 @@ public class Print {
|
|||||||
public static void printnb(Object obj) {
|
public static void printnb(Object obj) {
|
||||||
System.out.print(obj);
|
System.out.print(obj);
|
||||||
}
|
}
|
||||||
// The new Java SE5 printf() (from C):
|
// The Java SE5 printf() (from C):
|
||||||
public static PrintStream
|
public static PrintStream
|
||||||
printf(String format, Object... args) {
|
printf(String format, Object... args) {
|
||||||
return System.out.printf(format, args);
|
return System.out.printf(format, args);
|
||||||
|
@ -35,7 +35,7 @@ extends ArrayList<TaskItem<R,C>> {
|
|||||||
// Leave completed tasks for results reporting:
|
// Leave completed tasks for results reporting:
|
||||||
if(!item.future.isDone()) {
|
if(!item.future.isDone()) {
|
||||||
results.add("Cancelling " + item.task);
|
results.add("Cancelling " + item.task);
|
||||||
item.future.cancel(true); // May interrupt
|
item.future.cancel(true); // Can interrupt
|
||||||
items.remove();
|
items.remove();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//: operators/PassObject.java
|
//: operators/PassObject.java
|
||||||
// Passing objects to methods may not be
|
// Passing objects to methods might not be
|
||||||
// what you're used to.
|
// what you're used to.
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ public class Receipt {
|
|||||||
}
|
}
|
||||||
public void print(String name, int qty, double price) {
|
public void print(String name, int qty, double price) {
|
||||||
f.format("%-15.15s %5d %10.2f\n", name, qty, price);
|
f.format("%-15.15s %5d %10.2f\n", name, qty, price);
|
||||||
total += price;
|
total += price * qty;
|
||||||
}
|
}
|
||||||
public void printTotal() {
|
public void printTotal() {
|
||||||
f.format("%-15s %5s %10.2f\n", "Tax", "", total*0.06);
|
f.format("%-15s %5s %10.2f\n", "Tax", "", total*0.06);
|
||||||
@ -32,7 +32,7 @@ Item Qty Price
|
|||||||
Jack's Magic Be 4 4.25
|
Jack's Magic Be 4 4.25
|
||||||
Princess Peas 3 5.10
|
Princess Peas 3 5.10
|
||||||
Three Bears Por 1 14.29
|
Three Bears Por 1 14.29
|
||||||
Tax 1.42
|
Tax 2.80
|
||||||
-----
|
-----
|
||||||
Total 25.06
|
Total 49.39
|
||||||
*///:~
|
*///:~
|
@ -1,5 +1,5 @@
|
|||||||
//: strings/TestRegularExpression.java
|
//: strings/TestRegularExpression.java
|
||||||
// Allows you to easily try out regular expressions.
|
// Easily try out regular expressions.
|
||||||
// {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
|
// {Args: abcabcabcdefabc "abc+" "(abc)+" "(abc){2,}" }
|
||||||
import java.util.regex.*;
|
import java.util.regex.*;
|
||||||
import static net.mindview.util.Print.*;
|
import static net.mindview.util.Print.*;
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#! py -3
|
#! py -3
|
||||||
"""
|
"""
|
||||||
|
TODO: Make sure there's a newline at the end of the extracted files
|
||||||
|
|
||||||
Extract code examples from TIJ4 Refreshed. Extracts from plain text file
|
Extract code examples from TIJ4 Refreshed. Extracts from plain text file
|
||||||
"""
|
"""
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
@ -30,11 +30,11 @@ public class PetCount {
|
|||||||
counter.count("Pug");
|
counter.count("Pug");
|
||||||
if(pet instanceof Cat)
|
if(pet instanceof Cat)
|
||||||
counter.count("Cat");
|
counter.count("Cat");
|
||||||
if(pet instanceof Manx)
|
if(pet instanceof EgyptianMau)
|
||||||
counter.count("EgyptianMau");
|
counter.count("EgyptianMau");
|
||||||
if(pet instanceof Manx)
|
if(pet instanceof Manx)
|
||||||
counter.count("Manx");
|
counter.count("Manx");
|
||||||
if(pet instanceof Manx)
|
if(pet instanceof Cymric)
|
||||||
counter.count("Cymric");
|
counter.count("Cymric");
|
||||||
if(pet instanceof Rodent)
|
if(pet instanceof Rodent)
|
||||||
counter.count("Rodent");
|
counter.count("Rodent");
|
||||||
@ -54,5 +54,5 @@ public class PetCount {
|
|||||||
}
|
}
|
||||||
} /* Output:
|
} /* Output:
|
||||||
Rat Manx Cymric Mutt Pug Cymric Pug Manx Cymric Rat EgyptianMau Hamster EgyptianMau Mutt Mutt Cymric Mouse Pug Mouse Cymric
|
Rat Manx Cymric Mutt Pug Cymric Pug Manx Cymric Rat EgyptianMau Hamster EgyptianMau Mutt Mutt Cymric Mouse Pug Mouse Cymric
|
||||||
{Pug=3, Cat=9, Hamster=1, Cymric=7, Mouse=2, Mutt=3, Rodent=5, Pet=20, Manx=7, EgyptianMau=7, Dog=6, Rat=2}
|
{Rat=2, Cymric=5, Cat=9, Pet=20, Dog=6, Manx=7, EgyptianMau=2, Pug=3, Mouse=2, Rodent=5, Hamster=1, Mutt=3}
|
||||||
*///:~
|
*///:~
|
@ -1,4 +1,4 @@
|
|||||||
//: xml/Person.java
|
//: xml/APerson.java
|
||||||
// Use the XOM library to write and read XML
|
// Use the XOM library to write and read XML
|
||||||
// {Requires: nu.xom.Node; You must install
|
// {Requires: nu.xom.Node; You must install
|
||||||
// the XOM library from http://www.xom.nu }
|
// the XOM library from http://www.xom.nu }
|
||||||
@ -6,13 +6,13 @@ import nu.xom.*;
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class Person {
|
public class APerson {
|
||||||
private String first, last;
|
private String first, last;
|
||||||
public Person(String first, String last) {
|
public APerson(String first, String last) {
|
||||||
this.first = first;
|
this.first = first;
|
||||||
this.last = last;
|
this.last = last;
|
||||||
}
|
}
|
||||||
// Produce an XML Element from this Person object:
|
// Produce an XML Element from this APerson object:
|
||||||
public Element getXML() {
|
public Element getXML() {
|
||||||
Element person = new Element("person");
|
Element person = new Element("person");
|
||||||
Element firstName = new Element("first");
|
Element firstName = new Element("first");
|
||||||
@ -23,8 +23,8 @@ public class Person {
|
|||||||
person.appendChild(lastName);
|
person.appendChild(lastName);
|
||||||
return person;
|
return person;
|
||||||
}
|
}
|
||||||
// Constructor to restore a Person from an XML Element:
|
// Constructor to restore a APerson from an XML Element:
|
||||||
public Person(Element person) {
|
public APerson(Element person) {
|
||||||
first= person.getFirstChildElement("first").getValue();
|
first= person.getFirstChildElement("first").getValue();
|
||||||
last = person.getFirstChildElement("last").getValue();
|
last = person.getFirstChildElement("last").getValue();
|
||||||
}
|
}
|
||||||
@ -39,13 +39,13 @@ public class Person {
|
|||||||
serializer.flush();
|
serializer.flush();
|
||||||
}
|
}
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
List<Person> people = Arrays.asList(
|
List<APerson> people = Arrays.asList(
|
||||||
new Person("Dr. Bunsen", "Honeydew"),
|
new APerson("Dr. Bunsen", "Honeydew"),
|
||||||
new Person("Gonzo", "The Great"),
|
new APerson("Gonzo", "The Great"),
|
||||||
new Person("Phillip J.", "Fry"));
|
new APerson("Phillip J.", "Fry"));
|
||||||
System.out.println(people);
|
System.out.println(people);
|
||||||
Element root = new Element("people");
|
Element root = new Element("people");
|
||||||
for(Person p : people)
|
for(APerson p : people)
|
||||||
root.appendChild(p.getXML());
|
root.appendChild(p.getXML());
|
||||||
Document doc = new Document(root);
|
Document doc = new Document(root);
|
||||||
format(System.out, doc);
|
format(System.out, doc);
|
@ -1,18 +1,18 @@
|
|||||||
//: xml/People.java
|
//: xml/People.java
|
||||||
// {Requires: nu.xom.Node; You must install
|
// {Requires: nu.xom.Node; You must install
|
||||||
// the XOM library from http://www.xom.nu }
|
// the XOM library from http://www.xom.nu }
|
||||||
// {RunFirst: Person}
|
// {RunFirst: APerson}
|
||||||
import nu.xom.*;
|
import nu.xom.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class People extends ArrayList<Person> {
|
public class People extends ArrayList<APerson> {
|
||||||
public People(String fileName) throws Exception {
|
public People(String fileName) throws Exception {
|
||||||
Document doc = new Builder().build(new File(fileName));
|
Document doc = new Builder().build(new File(fileName));
|
||||||
Elements elements =
|
Elements elements =
|
||||||
doc.getRootElement().getChildElements();
|
doc.getRootElement().getChildElements();
|
||||||
for(int i = 0; i < elements.size(); i++)
|
for(int i = 0; i < elements.size(); i++)
|
||||||
add(new Person(elements.get(i)));
|
add(new APerson(elements.get(i)));
|
||||||
}
|
}
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
People p = new People("People.xml");
|
People p = new People("People.xml");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user