├── LICENSE.txt ├── 9781484216996.jpg ├── appb ├── SlideShow │ ├── ss │ │ ├── 67p.jpg │ │ ├── cv.jpg │ │ ├── em.jpg │ │ ├── ida.jpg │ │ ├── io.jpg │ │ ├── iss.jpg │ │ ├── kb.jpg │ │ ├── ncc.jpg │ │ ├── nix.jpg │ │ ├── pan.jpg │ │ ├── sol.jpg │ │ ├── ariel.jpg │ │ ├── atlas.jpg │ │ ├── ceres.jpg │ │ ├── ceres2.jpg │ │ ├── ceres3.jpg │ │ ├── ceres4.jpg │ │ ├── ceres5.jpg │ │ ├── charon.jpg │ │ ├── deimos.jpg │ │ ├── dione.jpg │ │ ├── dione2.jpg │ │ ├── dione3.jpg │ │ ├── dione4.jpg │ │ ├── earth.jpg │ │ ├── earth2.jpg │ │ ├── earth3.jpg │ │ ├── edge.jpg │ │ ├── emmars.jpg │ │ ├── eris.jpg │ │ ├── europa.jpg │ │ ├── gaspra.jpg │ │ ├── halley.jpg │ │ ├── halnuc.jpg │ │ ├── helene.jpg │ │ ├── hydra.jpg │ │ ├── janus.jpg │ │ ├── jupsys.jpg │ │ ├── mars.jpg │ │ ├── metis.jpg │ │ ├── mimas.jpg │ │ ├── moon.jpg │ │ ├── naiad.jpg │ │ ├── nepsys.jpg │ │ ├── nereid.jpg │ │ ├── oberon.jpg │ │ ├── oort.jpg │ │ ├── phobos.jpg │ │ ├── phoebe.jpg │ │ ├── pluto.jpg │ │ ├── pluto2.jpg │ │ ├── puck.jpg │ │ ├── quaoar.jpg │ │ ├── rhea.jpg │ │ ├── satsys.jpg │ │ ├── saturn.jpg │ │ ├── sedna.jpg │ │ ├── sol2.jpg │ │ ├── sol3.jpg │ │ ├── tethys.jpg │ │ ├── thebe.jpg │ │ ├── titan.jpg │ │ ├── triton.jpg │ │ ├── uranus.jpg │ │ ├── urasys.jpg │ │ ├── venus.jpg │ │ ├── vesta.jpg │ │ ├── adrastea.jpg │ │ ├── amalthea.jpg │ │ ├── callisto.jpg │ │ ├── calypso.jpg │ │ ├── charsurf.jpg │ │ ├── despina.jpg │ │ ├── earthsys.jpg │ │ ├── galatea.jpg │ │ ├── ganymede.jpg │ │ ├── hyperion.jpg │ │ ├── iapetus.jpg │ │ ├── ioeuropa.jpg │ │ ├── jupclose.jpg │ │ ├── jupiter.jpg │ │ ├── larissa.jpg │ │ ├── makemake.jpg │ │ ├── marsgold.jpg │ │ ├── marspole.jpg │ │ ├── marssys.jpg │ │ ├── marsurf1.jpg │ │ ├── marsurf2.jpg │ │ ├── marsurf3.jpg │ │ ├── marsurf4.jpg │ │ ├── marsurf5.jpg │ │ ├── marsurf6.jpg │ │ ├── marsurf7.jpg │ │ ├── mercury.jpg │ │ ├── mercury2.jpg │ │ ├── miranda.jpg │ │ ├── neptune.jpg │ │ ├── pandora.jpg │ │ ├── plutchar.jpg │ │ ├── plutosys.jpg │ │ ├── plutsil.jpg │ │ ├── plutsys.jpg │ │ ├── proteus.jpg │ │ ├── saturndm.jpg │ │ ├── saturnnp.jpg │ │ ├── telesto.jpg │ │ ├── thalassa.jpg │ │ ├── titania.jpg │ │ ├── titsurf.jpg │ │ ├── umbriel.jpg │ │ ├── uranus2.jpg │ │ ├── vensurf1.jpg │ │ ├── vensurf2.jpg │ │ ├── vensurf3.jpg │ │ ├── buzzaldrin.jpg │ │ ├── earthrise.jpg │ │ ├── enceladus.jpg │ │ ├── enceladus2.jpg │ │ ├── enceladus3.jpg │ │ ├── enceladus4.jpg │ │ ├── epimetheus.jpg │ │ ├── hyperion2.jpg │ │ ├── mercsurf1.jpg │ │ ├── mercsurf2.jpg │ │ ├── mercsurf3.jpg │ │ ├── mercsurf4.jpg │ │ ├── mercsurf5.jpg │ │ ├── moonsurf1.jpg │ │ ├── moonsurf2.jpg │ │ ├── plutsurf1.jpg │ │ ├── plutsurf2.jpg │ │ ├── plutsurf3.jpg │ │ ├── plutsurf4.jpg │ │ ├── prometheus.jpg │ │ ├── vesta-snowman.jpg │ │ ├── vesta-southpole.jpg │ │ └── index │ └── v1 │ │ └── SlideShow.java ├── Counter │ └── Counter.java └── ViewPage │ ├── v1 │ └── ViewPage.java │ ├── v2 │ └── ViewPage.java │ ├── v4 │ └── ViewPage.java │ └── v3 │ └── ViewPage.java ├── ch08 ├── ID │ ├── v1 │ │ └── ID.java │ └── v2 │ │ └── ID.java ├── MatMult │ ├── v1 │ │ ├── Matrix.java │ │ └── MatMult.java │ └── v2 │ │ ├── Matrix.java │ │ └── MatMult.java ├── CSDemo │ └── CSDemo.java └── PC │ └── PC.java ├── ch02 ├── UsePlanets │ ├── UsePlanets.java │ └── Planets.java ├── ID │ ├── v3 │ │ └── ID.java │ ├── v4 │ │ └── ID.java │ ├── v1 │ │ └── ID.java │ └── v2 │ │ └── ID.java ├── ThreadStopping │ ├── v1 │ │ └── ThreadStopping.java │ ├── v4 │ │ └── ThreadStopping.java │ ├── v2 │ │ └── ThreadStopping.java │ └── v3 │ │ └── ThreadStopping.java ├── DeadlockDemo │ └── DeadlockDemo.java └── JoinDemo │ ├── v2 │ └── JoinDemo.java │ └── v1 │ └── JoinDemo.java ├── ch04 ├── ExceptionThread │ ├── v1 │ │ └── ExceptionThread.java │ └── v2 │ │ └── ExceptionThread.java ├── TimerDemo │ ├── v2 │ │ └── TimerDemo.java │ └── v1 │ │ └── TimerDemo.java ├── ThreadLocalDemo │ └── ThreadLocalDemo.java └── InheritableThreadLocalDemo │ └── InheritableThreadLocalDemo.java ├── appa ├── ch07 │ └── ID │ │ ├── TestID.java │ │ └── ID.java ├── ch05 │ └── CountingThreads │ │ ├── v1 │ │ └── CountingThreads.java │ │ └── v2 │ │ └── CountingThreads.java ├── ch01 │ └── IntSleep │ │ └── IntSleep.java ├── ch02 │ └── CheckingAccount │ │ └── CheckingAccount.java ├── ch04 │ └── BackAndForth │ │ └── BackAndForth.java ├── ch06 │ └── PC │ │ └── PC.java └── ch03 │ └── Await │ └── Await.java ├── README.md ├── contributing.md ├── ch01 └── ThreadDemo │ ├── v4 │ └── ThreadDemo.java │ ├── v2 │ └── ThreadDemo.java │ ├── v1 │ └── ThreadDemo.java │ └── v3 │ └── ThreadDemo.java ├── ch03 └── PC │ ├── v1 │ └── PC.java │ └── v2 │ └── PC.java ├── ch07 ├── RLDemo │ └── RLDemo.java ├── PC │ └── PC.java └── Dictionary │ └── Dictionary.java ├── ch06 ├── PhaserDemo │ └── PhaserDemo.java ├── CountDownLatchDemo │ └── CountDownLatchDemo.java ├── SemaphoreDemo │ └── SemaphoreDemo.java ├── CyclicBarrierDemo │ └── CyclicBarrierDemo.java └── ExchangerDemo │ └── ExchangerDemo.java └── ch05 └── CalculateE └── CalculateE.java /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/LICENSE.txt -------------------------------------------------------------------------------- /9781484216996.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/9781484216996.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/67p.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/67p.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/cv.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/cv.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/em.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/em.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ida.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ida.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/io.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/io.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/iss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/iss.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/kb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/kb.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ncc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ncc.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/nix.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/nix.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/pan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/pan.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/sol.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/sol.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ariel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ariel.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/atlas.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/atlas.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ceres.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ceres.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ceres2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ceres2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ceres3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ceres3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ceres4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ceres4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ceres5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ceres5.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/charon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/charon.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/deimos.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/deimos.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/dione.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/dione.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/dione2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/dione2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/dione3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/dione3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/dione4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/dione4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/earth.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/earth.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/earth2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/earth2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/earth3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/earth3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/edge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/edge.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/emmars.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/emmars.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/eris.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/eris.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/europa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/europa.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/gaspra.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/gaspra.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/halley.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/halley.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/halnuc.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/halnuc.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/helene.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/helene.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/hydra.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/hydra.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/janus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/janus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/jupsys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/jupsys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mars.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mars.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/metis.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/metis.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mimas.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mimas.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/moon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/moon.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/naiad.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/naiad.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/nepsys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/nepsys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/nereid.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/nereid.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/oberon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/oberon.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/oort.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/oort.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/phobos.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/phobos.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/phoebe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/phoebe.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/pluto.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/pluto.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/pluto2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/pluto2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/puck.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/puck.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/quaoar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/quaoar.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/rhea.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/rhea.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/satsys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/satsys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/saturn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/saturn.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/sedna.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/sedna.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/sol2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/sol2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/sol3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/sol3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/tethys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/tethys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/thebe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/thebe.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/titan.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/titan.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/triton.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/triton.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/uranus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/uranus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/urasys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/urasys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/venus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/venus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vesta.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vesta.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/adrastea.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/adrastea.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/amalthea.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/amalthea.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/callisto.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/callisto.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/calypso.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/calypso.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/charsurf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/charsurf.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/despina.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/despina.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/earthsys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/earthsys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/galatea.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/galatea.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ganymede.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ganymede.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/hyperion.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/hyperion.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/iapetus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/iapetus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/ioeuropa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/ioeuropa.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/jupclose.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/jupclose.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/jupiter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/jupiter.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/larissa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/larissa.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/makemake.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/makemake.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsgold.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsgold.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marspole.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marspole.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marssys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marssys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf1.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf5.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf6.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/marsurf7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/marsurf7.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercury.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercury.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercury2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercury2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/miranda.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/miranda.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/neptune.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/neptune.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/pandora.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/pandora.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutchar.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutchar.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutosys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutosys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsil.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsil.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsys.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsys.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/proteus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/proteus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/saturndm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/saturndm.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/saturnnp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/saturnnp.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/telesto.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/telesto.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/thalassa.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/thalassa.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/titania.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/titania.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/titsurf.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/titsurf.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/umbriel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/umbriel.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/uranus2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/uranus2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vensurf1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vensurf1.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vensurf2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vensurf2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vensurf3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vensurf3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/buzzaldrin.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/buzzaldrin.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/earthrise.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/earthrise.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/enceladus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/enceladus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/enceladus2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/enceladus2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/enceladus3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/enceladus3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/enceladus4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/enceladus4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/epimetheus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/epimetheus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/hyperion2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/hyperion2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercsurf1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercsurf1.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercsurf2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercsurf2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercsurf3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercsurf3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercsurf4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercsurf4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/mercsurf5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/mercsurf5.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/moonsurf1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/moonsurf1.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/moonsurf2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/moonsurf2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsurf1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsurf1.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsurf2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsurf2.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsurf3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsurf3.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/plutsurf4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/plutsurf4.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/prometheus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/prometheus.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vesta-snowman.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vesta-snowman.jpg -------------------------------------------------------------------------------- /appb/SlideShow/ss/vesta-southpole.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/java-threads-concurrency-utilities/HEAD/appb/SlideShow/ss/vesta-southpole.jpg -------------------------------------------------------------------------------- /ch08/ID/v1/ID.java: -------------------------------------------------------------------------------- 1 | class ID 2 | { 3 | private static volatile long nextID = 1; 4 | 5 | static synchronized long getNextID() 6 | { 7 | return nextID++; 8 | } 9 | } -------------------------------------------------------------------------------- /ch08/ID/v2/ID.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.atomic.AtomicLong; 2 | 3 | class ID 4 | { 5 | private static AtomicLong nextID = new AtomicLong(1); 6 | 7 | static long getNextID() 8 | { 9 | return nextID.getAndIncrement(); 10 | } 11 | } -------------------------------------------------------------------------------- /ch02/UsePlanets/UsePlanets.java: -------------------------------------------------------------------------------- 1 | public class UsePlanets 2 | { 3 | public static void main(String[] args) 4 | { 5 | Planets planets = new Planets(); 6 | System.out.println(planets.isPlanet("Earth")); 7 | System.out.println(planets.isPlanet("Vulcan")); 8 | } 9 | } -------------------------------------------------------------------------------- /ch02/ID/v3/ID.java: -------------------------------------------------------------------------------- 1 | public class ID 2 | { 3 | private static int counter; // initialized to 0 by default 4 | 5 | public static int getID() 6 | { 7 | return counter++; 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | System.out.println(ID.getID()); 13 | } 14 | } -------------------------------------------------------------------------------- /ch02/ID/v4/ID.java: -------------------------------------------------------------------------------- 1 | public class ID 2 | { 3 | private static int counter; // initialized to 0 by default 4 | 5 | public static synchronized int getID() 6 | { 7 | return counter++; 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | System.out.println(ID.getID()); 13 | } 14 | } -------------------------------------------------------------------------------- /ch02/ID/v1/ID.java: -------------------------------------------------------------------------------- 1 | public class ID 2 | { 3 | private int counter; // initialized to 0 by default 4 | 5 | public int getID() 6 | { 7 | return counter++; 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | ID id = new ID(); 13 | System.out.println(id.getID()); 14 | } 15 | } -------------------------------------------------------------------------------- /ch02/ID/v2/ID.java: -------------------------------------------------------------------------------- 1 | public class ID 2 | { 3 | private int counter; // initialized to 0 by default 4 | 5 | public synchronized int getID() 6 | { 7 | return counter++; 8 | } 9 | 10 | public static void main(String[] args) 11 | { 12 | ID id = new ID(); 13 | System.out.println(id.getID()); 14 | } 15 | } -------------------------------------------------------------------------------- /ch04/ExceptionThread/v1/ExceptionThread.java: -------------------------------------------------------------------------------- 1 | public class ExceptionThread 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = new Runnable() 6 | { 7 | @Override 8 | public void run() 9 | { 10 | int x = 1 / 0; // Line 10 11 | } 12 | }; 13 | Thread thd = new Thread(r); 14 | thd.start(); 15 | } 16 | } -------------------------------------------------------------------------------- /appa/ch07/ID/TestID.java: -------------------------------------------------------------------------------- 1 | public class TestID 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = () -> 6 | { 7 | Thread ct = Thread.currentThread(); 8 | while (true) 9 | System.out.printf("%s: %d%n", ct.getName(), 10 | ID.getID()); 11 | }; 12 | Thread thdA = new Thread(r, "A"); 13 | Thread thdB = new Thread(r, "B"); 14 | thdA.start(); 15 | thdB.start(); 16 | } 17 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Java Threads and the Concurrency Utilities*](http://www.apress.com/9781484216996) by JEFF FRIESEN (Apress, 2015). 4 | 5 | ![Cover image](9781484216996.jpg) 6 | 7 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 8 | 9 | ## Releases 10 | 11 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 12 | 13 | ## Contributions 14 | 15 | See the file Contributing.md for more information on how you can contribute to this repository. 16 | -------------------------------------------------------------------------------- /ch08/MatMult/v1/Matrix.java: -------------------------------------------------------------------------------- 1 | public class Matrix 2 | { 3 | private final int[][] matrix; 4 | 5 | public Matrix(int nrows, int ncols) 6 | { 7 | matrix = new int[nrows][ncols]; 8 | } 9 | 10 | public int getCols() 11 | { 12 | return matrix[0].length; 13 | } 14 | 15 | public int getRows() 16 | { 17 | return matrix.length; 18 | } 19 | 20 | public int getValue(int row, int col) 21 | { 22 | return matrix[row][col]; 23 | } 24 | 25 | public void setValue(int row, int col, int value) 26 | { 27 | matrix[row][col] = value; 28 | } 29 | } -------------------------------------------------------------------------------- /ch08/MatMult/v2/Matrix.java: -------------------------------------------------------------------------------- 1 | public class Matrix 2 | { 3 | private final int[][] matrix; 4 | 5 | public Matrix(int nrows, int ncols) 6 | { 7 | matrix = new int[nrows][ncols]; 8 | } 9 | 10 | public int getCols() 11 | { 12 | return matrix[0].length; 13 | } 14 | 15 | public int getRows() 16 | { 17 | return matrix.length; 18 | } 19 | 20 | public int getValue(int row, int col) 21 | { 22 | return matrix[row][col]; 23 | } 24 | 25 | public void setValue(int row, int col, int value) 26 | { 27 | matrix[row][col] = value; 28 | } 29 | } -------------------------------------------------------------------------------- /ch04/TimerDemo/v2/TimerDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.Timer; 2 | import java.util.TimerTask; 3 | 4 | public class TimerDemo 5 | { 6 | public static void main(String[] args) 7 | { 8 | TimerTask task = new TimerTask() 9 | { 10 | @Override 11 | public void run() 12 | { 13 | System.out.println(System.currentTimeMillis()); 14 | } 15 | }; 16 | Timer timer = new Timer(); 17 | timer.schedule(task, 0, 1000); 18 | } 19 | } -------------------------------------------------------------------------------- /ch02/UsePlanets/Planets.java: -------------------------------------------------------------------------------- 1 | import java.util.Set; 2 | import java.util.TreeSet; 3 | 4 | public final class Planets 5 | { 6 | private final Set planets = new TreeSet<>(); 7 | 8 | public Planets() 9 | { 10 | planets.add("Mercury"); 11 | planets.add("Venus"); 12 | planets.add("Earth"); 13 | planets.add("Mars"); 14 | planets.add("Jupiter"); 15 | planets.add("Saturn"); 16 | planets.add("Uranus"); 17 | planets.add("Neptune"); 18 | } 19 | 20 | public boolean isPlanet(String planetName) 21 | { 22 | return planets.contains(planetName); 23 | } 24 | } -------------------------------------------------------------------------------- /appa/ch07/ID/ID.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.locks.ReentrantLock; 2 | 3 | public class ID 4 | { 5 | private static int counter; // initialized to 0 by default 6 | 7 | private final static ReentrantLock lock = new ReentrantLock(); 8 | 9 | public static int getID() 10 | { 11 | lock.lock(); 12 | try 13 | { 14 | int temp = counter + 1; 15 | try 16 | { 17 | Thread.sleep(1); 18 | } 19 | catch (InterruptedException ie) 20 | { 21 | } 22 | return counter = temp; 23 | } 24 | finally 25 | { 26 | lock.unlock(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! -------------------------------------------------------------------------------- /ch04/TimerDemo/v1/TimerDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.Timer; 2 | import java.util.TimerTask; 3 | 4 | public class TimerDemo 5 | { 6 | public static void main(String[] args) 7 | { 8 | TimerTask task = new TimerTask() 9 | { 10 | @Override 11 | public void run() 12 | { 13 | System.out.println("alarm going off"); 14 | System.exit(0); 15 | } 16 | }; 17 | Timer timer = new Timer(); 18 | timer.schedule(task, 2000); // Execute one-shot timer task after 19 | // 2-second delay. 20 | } 21 | } -------------------------------------------------------------------------------- /ch02/ThreadStopping/v1/ThreadStopping.java: -------------------------------------------------------------------------------- 1 | public class ThreadStopping 2 | { 3 | public static void main(String[] args) 4 | { 5 | class StoppableThread extends Thread 6 | { 7 | private boolean stopped; // defaults to false 8 | 9 | @Override 10 | public void run() 11 | { 12 | while(!stopped) 13 | System.out.println("running"); 14 | } 15 | 16 | void stopThread() 17 | { 18 | stopped = true; 19 | } 20 | } 21 | StoppableThread thd = new StoppableThread(); 22 | thd.start(); 23 | try 24 | { 25 | Thread.sleep(1000); // sleep for 1 second 26 | } 27 | catch (InterruptedException ie) 28 | { 29 | } 30 | thd.stopThread(); 31 | } 32 | } -------------------------------------------------------------------------------- /appa/ch05/CountingThreads/v1/CountingThreads.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Executors; 2 | import java.util.concurrent.ExecutorService; 3 | 4 | public class CountingThreads 5 | { 6 | public static void main(String[] args) 7 | { 8 | Runnable r = new Runnable() 9 | { 10 | @Override 11 | public void run() 12 | { 13 | String name = Thread.currentThread().getName(); 14 | int count = 0; 15 | while (true) 16 | System.out.println(name + ": " + count++); 17 | } 18 | }; 19 | ExecutorService es = Executors.newFixedThreadPool(2); 20 | es.submit(r); 21 | es.submit(r); 22 | } 23 | } -------------------------------------------------------------------------------- /ch02/ThreadStopping/v4/ThreadStopping.java: -------------------------------------------------------------------------------- 1 | public class ThreadStopping 2 | { 3 | public static void main(String[] args) 4 | { 5 | class StoppableThread extends Thread 6 | { 7 | private volatile boolean stopped; // defaults to false 8 | 9 | @Override 10 | public void run() 11 | { 12 | while(!stopped) 13 | System.out.println("running"); 14 | } 15 | 16 | void stopThread() 17 | { 18 | stopped = true; 19 | } 20 | } 21 | StoppableThread thd = new StoppableThread(); 22 | thd.start(); 23 | try 24 | { 25 | Thread.sleep(1000); // sleep for 1 second 26 | } 27 | catch (InterruptedException ie) 28 | { 29 | } 30 | thd.stopThread(); 31 | } 32 | } -------------------------------------------------------------------------------- /ch02/ThreadStopping/v2/ThreadStopping.java: -------------------------------------------------------------------------------- 1 | public class ThreadStopping 2 | { 3 | public static void main(String[] args) 4 | { 5 | class StoppableThread extends Thread 6 | { 7 | private boolean stopped; // defaults to false 8 | 9 | @Override 10 | public void run() 11 | { 12 | synchronized(this) 13 | { 14 | while(!stopped) 15 | System.out.println("running"); 16 | } 17 | } 18 | 19 | synchronized void stopThread() 20 | { 21 | stopped = true; 22 | } 23 | } 24 | StoppableThread thd = new StoppableThread(); 25 | thd.start(); 26 | try 27 | { 28 | Thread.sleep(1000); // sleep for 1 second 29 | } 30 | catch (InterruptedException ie) 31 | { 32 | } 33 | thd.stopThread(); 34 | } 35 | } -------------------------------------------------------------------------------- /ch01/ThreadDemo/v4/ThreadDemo.java: -------------------------------------------------------------------------------- 1 | public class ThreadDemo 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = new Runnable() 6 | { 7 | @Override 8 | public void run() 9 | { 10 | String name = Thread.currentThread().getName(); 11 | int count = 0; 12 | while (!Thread.interrupted()) 13 | System.out.println(name + ": " + count++); 14 | } 15 | }; 16 | Thread thdA = new Thread(r); 17 | Thread thdB = new Thread(r); 18 | thdA.start(); 19 | thdB.start(); 20 | try 21 | { 22 | Thread.sleep(2000); 23 | } 24 | catch (InterruptedException ie) 25 | { 26 | } 27 | thdA.interrupt(); 28 | thdB.interrupt(); 29 | } 30 | } -------------------------------------------------------------------------------- /ch01/ThreadDemo/v2/ThreadDemo.java: -------------------------------------------------------------------------------- 1 | public class ThreadDemo 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = new Runnable() 6 | { 7 | @Override 8 | public void run() 9 | { 10 | String name = Thread.currentThread().getName(); 11 | int count = 0; 12 | while (!Thread.interrupted()) 13 | System.out.println(name + ": " + count++); 14 | } 15 | }; 16 | Thread thdA = new Thread(r); 17 | Thread thdB = new Thread(r); 18 | thdA.start(); 19 | thdB.start(); 20 | while (true) 21 | { 22 | double n = Math.random(); 23 | if (n >= 0.49999999 && n <= 0.50000001) 24 | break; 25 | } 26 | thdA.interrupt(); 27 | thdB.interrupt(); 28 | } 29 | } -------------------------------------------------------------------------------- /ch04/ThreadLocalDemo/ThreadLocalDemo.java: -------------------------------------------------------------------------------- 1 | public class ThreadLocalDemo 2 | { 3 | private static volatile ThreadLocal userID = 4 | new ThreadLocal(); 5 | 6 | public static void main(String[] args) 7 | { 8 | Runnable r = new Runnable() 9 | { 10 | @Override 11 | public void run() 12 | { 13 | String name = Thread.currentThread().getName(); 14 | if (name.equals("A")) 15 | userID.set("foxtrot"); 16 | else 17 | userID.set("charlie"); 18 | System.out.println(name + " " + userID.get()); 19 | } 20 | }; 21 | Thread thdA = new Thread(r); 22 | thdA.setName("A"); 23 | Thread thdB = new Thread(r); 24 | thdB.setName("B"); 25 | thdA.start(); 26 | thdB.start(); 27 | } 28 | } -------------------------------------------------------------------------------- /ch02/ThreadStopping/v3/ThreadStopping.java: -------------------------------------------------------------------------------- 1 | public class ThreadStopping 2 | { 3 | public static void main(String[] args) 4 | { 5 | class StoppableThread extends Thread 6 | { 7 | private boolean stopped; // defaults to false 8 | 9 | @Override 10 | public void run() 11 | { 12 | boolean _stopped = false; 13 | while (!_stopped) 14 | { 15 | synchronized(this) 16 | { 17 | _stopped = stopped; 18 | } 19 | System.out.println("running"); 20 | } 21 | } 22 | 23 | synchronized void stopThread() 24 | { 25 | stopped = true; 26 | } 27 | } 28 | StoppableThread thd = new StoppableThread(); 29 | thd.start(); 30 | try 31 | { 32 | Thread.sleep(1000); // sleep for 1 second 33 | } 34 | catch (InterruptedException ie) 35 | { 36 | } 37 | thd.stopThread(); 38 | } 39 | } -------------------------------------------------------------------------------- /ch04/InheritableThreadLocalDemo/InheritableThreadLocalDemo.java: -------------------------------------------------------------------------------- 1 | public class InheritableThreadLocalDemo 2 | { 3 | private static final InheritableThreadLocal intVal = 4 | new InheritableThreadLocal(); 5 | 6 | public static void main(String[] args) 7 | { 8 | Runnable rP = () -> 9 | { 10 | intVal.set(new Integer(10)); 11 | Runnable rC = () -> 12 | { 13 | Thread thd = Thread.currentThread(); 14 | String name = thd.getName(); 15 | System.out.printf("%s %d%n", name, 16 | intVal.get()); 17 | }; 18 | Thread thdChild = new Thread(rC); 19 | thdChild.setName("Child"); 20 | thdChild.start(); 21 | }; 22 | new Thread(rP).start(); 23 | } 24 | } -------------------------------------------------------------------------------- /appa/ch01/IntSleep/IntSleep.java: -------------------------------------------------------------------------------- 1 | public class IntSleep 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = new Runnable() 6 | { 7 | @Override 8 | public void run() 9 | { 10 | while (true) 11 | { 12 | System.out.println("hello"); 13 | try 14 | { 15 | Thread.sleep(100); 16 | } 17 | catch (InterruptedException ie) 18 | { 19 | System.out.println("interrupted"); 20 | break; 21 | } 22 | } 23 | } 24 | }; 25 | Thread t = new Thread(r); 26 | t.start(); 27 | try 28 | { 29 | Thread.sleep(2000); 30 | } 31 | catch (InterruptedException ie) 32 | { 33 | } 34 | t.interrupt(); 35 | } 36 | } -------------------------------------------------------------------------------- /appa/ch05/CountingThreads/v2/CountingThreads.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Executors; 2 | import java.util.concurrent.ExecutorService; 3 | import java.util.concurrent.ThreadFactory; 4 | 5 | public class CountingThreads 6 | { 7 | public static void main(String[] args) 8 | { 9 | Runnable r = new Runnable() 10 | { 11 | @Override 12 | public void run() 13 | { 14 | String name = Thread.currentThread().getName(); 15 | int count = 0; 16 | while (true) 17 | System.out.println(name + ": " + count++); 18 | } 19 | }; 20 | ExecutorService es = 21 | Executors.newSingleThreadExecutor(new NamedThread("A")); 22 | es.submit(r); 23 | es = Executors.newSingleThreadExecutor(new NamedThread("B")); 24 | es.submit(r); 25 | } 26 | } 27 | 28 | class NamedThread implements ThreadFactory 29 | { 30 | private volatile String name; // newThread() could be called by a 31 | // different thread 32 | 33 | NamedThread(String name) 34 | { 35 | this.name = name; 36 | } 37 | 38 | @Override 39 | public Thread newThread(Runnable r) 40 | { 41 | return new Thread(r, name); 42 | } 43 | } -------------------------------------------------------------------------------- /ch04/ExceptionThread/v2/ExceptionThread.java: -------------------------------------------------------------------------------- 1 | public class ExceptionThread 2 | { 3 | public static void main(String[] args) 4 | { 5 | Runnable r = new Runnable() 6 | { 7 | @Override 8 | public void run() 9 | { 10 | int x = 1 / 0; 11 | } 12 | }; 13 | Thread thd = new Thread(r); 14 | Thread.UncaughtExceptionHandler uceh; 15 | uceh = new Thread.UncaughtExceptionHandler() 16 | { 17 | @Override 18 | public void uncaughtException(Thread t, Throwable e) 19 | { 20 | System.out.println("Caught throwable " + e + 21 | " for thread " + t); 22 | } 23 | }; 24 | thd.setUncaughtExceptionHandler(uceh); 25 | uceh = new Thread.UncaughtExceptionHandler() 26 | { 27 | @Override 28 | public void uncaughtException(Thread t, Throwable e) 29 | { 30 | System.out.println("Default uncaught exception handler"); 31 | System.out.println("Caught throwable " + e + 32 | " for thread " + t); 33 | } 34 | }; 35 | thd.setDefaultUncaughtExceptionHandler(uceh); 36 | thd.start(); 37 | } 38 | } -------------------------------------------------------------------------------- /ch08/MatMult/v1/MatMult.java: -------------------------------------------------------------------------------- 1 | public class MatMult 2 | { 3 | public static void main(String[] args) 4 | { 5 | Matrix a = new Matrix(1, 3); 6 | a.setValue(0, 0, 1); // | 1 2 3 | 7 | a.setValue(0, 1, 2); 8 | a.setValue(0, 2, 3); 9 | dump(a); 10 | Matrix b = new Matrix(3, 2); 11 | b.setValue(0, 0, 4); // | 4 7 | 12 | b.setValue(1, 0, 5); // | 5 8 | 13 | b.setValue(2, 0, 6); // | 6 9 | 14 | b.setValue(0, 1, 7); 15 | b.setValue(1, 1, 8); 16 | b.setValue(2, 1, 9); 17 | dump(b); 18 | dump(multiply(a, b)); 19 | } 20 | 21 | public static void dump(Matrix m) 22 | { 23 | for (int i = 0; i < m.getRows(); i++) 24 | { 25 | for (int j = 0; j < m.getCols(); j++) 26 | System.out.printf("%d ", m.getValue(i, j)); 27 | System.out.println(); 28 | } 29 | System.out.println(); 30 | } 31 | 32 | public static Matrix multiply(Matrix a, Matrix b) 33 | { 34 | if (a.getCols() != b.getRows()) 35 | throw new IllegalArgumentException("rows/columns mismatch"); 36 | Matrix result = new Matrix(a.getRows(), b.getCols()); 37 | for (int i = 0; i < a.getRows(); i++) 38 | for (int j = 0; j < b.getCols(); j++) 39 | for (int k = 0; k < a.getCols(); k++) 40 | result.setValue(i, j, result.getValue(i, j) + 41 | a.getValue(i, k) * b.getValue(k, j)); 42 | return result; 43 | } 44 | } -------------------------------------------------------------------------------- /appa/ch02/CheckingAccount/CheckingAccount.java: -------------------------------------------------------------------------------- 1 | public class CheckingAccount 2 | { 3 | private volatile int balance; 4 | 5 | public CheckingAccount(int initialBalance) 6 | { 7 | balance = initialBalance; 8 | } 9 | 10 | public synchronized boolean withdraw(int amount) 11 | { 12 | if (amount <= balance) 13 | { 14 | try 15 | { 16 | Thread.sleep((int) (Math.random() * 200)); 17 | } 18 | catch (InterruptedException ie) 19 | { 20 | } 21 | balance -= amount; 22 | return true; 23 | } 24 | return false; 25 | } 26 | 27 | public static void main(String[] args) 28 | { 29 | final CheckingAccount ca = new CheckingAccount(100); 30 | Runnable r = new Runnable() 31 | { 32 | @Override 33 | public void run() 34 | { 35 | String name = Thread.currentThread().getName(); 36 | for (int i = 0; i < 10; i++) 37 | System.out.println (name + " withdraws $10: " + 38 | ca.withdraw(10)); 39 | } 40 | }; 41 | Thread thdHusband = new Thread(r); 42 | thdHusband.setName("Husband"); 43 | Thread thdWife = new Thread(r); 44 | thdWife.setName("Wife"); 45 | thdHusband.start(); 46 | thdWife.start(); 47 | } 48 | } -------------------------------------------------------------------------------- /ch01/ThreadDemo/v1/ThreadDemo.java: -------------------------------------------------------------------------------- 1 | public class ThreadDemo 2 | { 3 | public static void main(String[] args) 4 | { 5 | boolean isDaemon = args.length != 0; 6 | Runnable r = new Runnable() 7 | { 8 | @Override 9 | public void run() 10 | { 11 | Thread thd = Thread.currentThread(); 12 | while (true) 13 | System.out.printf("%s is %salive and in %s " + 14 | "state%n", 15 | thd.getName(), 16 | thd.isAlive() ? "" : "not ", 17 | thd.getState()); 18 | } 19 | }; 20 | Thread t1 = new Thread(r, "thd1"); 21 | if (isDaemon) 22 | t1.setDaemon(true); 23 | System.out.printf("%s is %salive and in %s state%n", 24 | t1.getName(), 25 | t1.isAlive() ? "" : "not ", 26 | t1.getState()); 27 | Thread t2 = new Thread(r); 28 | t2.setName("thd2"); 29 | if (isDaemon) 30 | t2.setDaemon(true); 31 | System.out.printf("%s is %salive and in %s state%n", 32 | t2.getName(), 33 | t2.isAlive() ? "" : "not ", 34 | t2.getState()); 35 | t1.start(); 36 | t2.start(); 37 | } 38 | } -------------------------------------------------------------------------------- /appb/Counter/Counter.java: -------------------------------------------------------------------------------- 1 | import java.awt.EventQueue; 2 | import java.awt.FlowLayout; 3 | 4 | import java.awt.event.ActionListener; 5 | 6 | import javax.swing.JButton; 7 | import javax.swing.JFrame; 8 | import javax.swing.JLabel; 9 | import javax.swing.JPanel; 10 | import javax.swing.Timer; 11 | 12 | public class Counter extends JFrame 13 | { 14 | int count; 15 | 16 | public Counter(String title) 17 | { 18 | super(title); 19 | setDefaultCloseOperation(EXIT_ON_CLOSE); 20 | 21 | JPanel pnl = new JPanel(); 22 | ((FlowLayout) pnl.getLayout()).setHgap(20); 23 | final JLabel lblCount = new JLabel(""); 24 | pnl.add(lblCount); 25 | final JButton btnStartStop = new JButton("Start"); 26 | ActionListener al = (ae) -> 27 | { 28 | ++count; 29 | lblCount.setText(count + " "); 30 | }; 31 | final Timer timer = new Timer(30, al); 32 | al = (ae) -> 33 | { 34 | if (btnStartStop.getText().equals("Start")) 35 | { 36 | btnStartStop.setText("Stop"); 37 | timer.start(); 38 | } 39 | else 40 | { 41 | btnStartStop.setText("Start"); 42 | timer.stop(); 43 | } 44 | }; 45 | btnStartStop.addActionListener(al); 46 | pnl.add(btnStartStop); 47 | setContentPane(pnl); 48 | 49 | setSize(300, 80); 50 | setVisible(true); 51 | } 52 | 53 | public static void main(String[] args) 54 | { 55 | EventQueue.invokeLater(() -> new Counter("Counter")); 56 | } 57 | } -------------------------------------------------------------------------------- /appa/ch04/BackAndForth/BackAndForth.java: -------------------------------------------------------------------------------- 1 | import java.util.Timer; 2 | import java.util.TimerTask; 3 | 4 | public class BackAndForth 5 | { 6 | static enum Direction { FORWARDS, BACKWARDS } 7 | 8 | public static void main(String[] args) 9 | { 10 | TimerTask task = new TimerTask() 11 | { 12 | final static int MAXSTEPS = 20; 13 | 14 | volatile Direction direction = Direction.FORWARDS; 15 | 16 | volatile int steps = 0; 17 | 18 | @Override 19 | public void run() 20 | { 21 | switch (direction) 22 | { 23 | case FORWARDS : System.out.print("\b "); 24 | System.out.print("*"); 25 | break; 26 | 27 | case BACKWARDS: System.out.print("\b "); 28 | System.out.print("\b\b*"); 29 | } 30 | 31 | if (++steps == MAXSTEPS) 32 | { 33 | direction = 34 | (direction == Direction.FORWARDS) 35 | ? Direction.BACKWARDS 36 | : Direction.FORWARDS; 37 | steps = 0; 38 | } 39 | } 40 | }; 41 | Timer timer = new Timer(); 42 | timer.schedule(task, 0, 100); 43 | } 44 | } -------------------------------------------------------------------------------- /ch03/PC/v1/PC.java: -------------------------------------------------------------------------------- 1 | public class PC 2 | { 3 | public static void main(String[] args) 4 | { 5 | Shared s = new Shared(); 6 | new Producer(s).start(); 7 | new Consumer(s).start(); 8 | } 9 | } 10 | 11 | class Shared 12 | { 13 | private char c; 14 | private volatile boolean writeable = true; 15 | 16 | synchronized void setSharedChar(char c) 17 | { 18 | while (!writeable) 19 | try 20 | { 21 | wait(); 22 | } 23 | catch (InterruptedException ie) 24 | { 25 | } 26 | this.c = c; 27 | writeable = false; 28 | notify(); 29 | } 30 | 31 | synchronized char getSharedChar() 32 | { 33 | while (writeable) 34 | try 35 | { 36 | wait(); 37 | } 38 | catch (InterruptedException ie) 39 | { 40 | } 41 | writeable = true; 42 | notify(); 43 | return c; 44 | } 45 | } 46 | 47 | class Producer extends Thread 48 | { 49 | private final Shared s; 50 | 51 | Producer(Shared s) 52 | { 53 | this.s = s; 54 | } 55 | 56 | @Override 57 | public void run() 58 | { 59 | for (char ch = 'A'; ch <= 'Z'; ch++) 60 | { 61 | s.setSharedChar(ch); 62 | System.out.println(ch + " produced by producer."); 63 | } 64 | } 65 | } 66 | class Consumer extends Thread 67 | { 68 | private final Shared s; 69 | 70 | Consumer(Shared s) 71 | { 72 | this.s = s; 73 | } 74 | 75 | @Override 76 | public void run() 77 | { 78 | char ch; 79 | do 80 | { 81 | ch = s.getSharedChar(); 82 | System.out.println(ch + " consumed by consumer."); 83 | } 84 | while (ch != 'Z'); 85 | } 86 | } -------------------------------------------------------------------------------- /ch07/RLDemo/RLDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Executors; 2 | import java.util.concurrent.ExecutorService; 3 | import java.util.concurrent.TimeUnit; 4 | 5 | import java.util.concurrent.locks.ReentrantLock; 6 | 7 | public class RLDemo 8 | { 9 | public static void main(String[] args) 10 | { 11 | ExecutorService executor = Executors.newFixedThreadPool(2); 12 | final ReentrantLock lock = new ReentrantLock(); 13 | 14 | class Worker implements Runnable 15 | { 16 | private final String name; 17 | 18 | Worker(String name) 19 | { 20 | this.name = name; 21 | } 22 | 23 | @Override 24 | public void run() 25 | { 26 | lock.lock(); 27 | try 28 | { 29 | if (lock.isHeldByCurrentThread()) 30 | System.out.printf("Thread %s entered critical section.%n", 31 | name); 32 | System.out.printf("Thread %s performing work.%n", name); 33 | try 34 | { 35 | Thread.sleep(2000); 36 | } 37 | catch (InterruptedException ie) 38 | { 39 | ie.printStackTrace(); 40 | } 41 | System.out.printf("Thread %s finished working.%n", name); 42 | } 43 | finally 44 | { 45 | lock.unlock(); 46 | } 47 | } 48 | } 49 | executor.execute(new Worker("ThdA")); 50 | executor.execute(new Worker("ThdB")); 51 | try 52 | { 53 | executor.awaitTermination(5, TimeUnit.SECONDS); 54 | } 55 | catch (InterruptedException ie) 56 | { 57 | ie.printStackTrace(); 58 | } 59 | executor.shutdownNow(); 60 | } 61 | } -------------------------------------------------------------------------------- /ch03/PC/v2/PC.java: -------------------------------------------------------------------------------- 1 | public class PC 2 | { 3 | public static void main(String[] args) 4 | { 5 | Shared s = new Shared(); 6 | new Producer(s).start(); 7 | new Consumer(s).start(); 8 | } 9 | } 10 | 11 | class Shared 12 | { 13 | private char c; 14 | private volatile boolean writeable = true; 15 | 16 | synchronized void setSharedChar(char c) 17 | { 18 | while (!writeable) 19 | try 20 | { 21 | wait(); 22 | } 23 | catch (InterruptedException ie) 24 | { 25 | } 26 | this.c = c; 27 | writeable = false; 28 | notify(); 29 | } 30 | 31 | synchronized char getSharedChar() 32 | { 33 | while (writeable) 34 | try 35 | { 36 | wait(); 37 | } 38 | catch (InterruptedException ie) 39 | { 40 | } 41 | writeable = true; 42 | notify(); 43 | return c; 44 | } 45 | } 46 | 47 | class Producer extends Thread 48 | { 49 | private final Shared s; 50 | 51 | Producer(Shared s) 52 | { 53 | this.s = s; 54 | } 55 | 56 | @Override 57 | public void run() 58 | { 59 | for (char ch = 'A'; ch <= 'Z'; ch++) 60 | { 61 | synchronized(s) 62 | { 63 | s.setSharedChar(ch); 64 | System.out.println(ch + " produced by producer."); 65 | } 66 | } 67 | } 68 | } 69 | class Consumer extends Thread 70 | { 71 | private final Shared s; 72 | 73 | Consumer(Shared s) 74 | { 75 | this.s = s; 76 | } 77 | 78 | @Override 79 | public void run() 80 | { 81 | char ch; 82 | do 83 | { 84 | synchronized(s) 85 | { 86 | ch = s.getSharedChar(); 87 | System.out.println(ch + " consumed by consumer."); 88 | } 89 | } 90 | while (ch != 'Z'); 91 | } 92 | } -------------------------------------------------------------------------------- /ch08/CSDemo/CSDemo.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.math.MathContext; 3 | import java.math.RoundingMode; 4 | 5 | import java.util.concurrent.Callable; 6 | import java.util.concurrent.CompletionService; 7 | import java.util.concurrent.ExecutorCompletionService; 8 | import java.util.concurrent.Executors; 9 | import java.util.concurrent.ExecutorService; 10 | import java.util.concurrent.Future; 11 | 12 | public class CSDemo 13 | { 14 | public static void main(String[] args) throws Exception 15 | { 16 | ExecutorService es = Executors.newFixedThreadPool(10); 17 | CompletionService cs = 18 | new ExecutorCompletionService(es); 19 | cs.submit(new CalculateE(17)); 20 | cs.submit(new CalculateE(170)); 21 | Future result = cs.take(); 22 | System.out.println(result.get()); 23 | System.out.println(); 24 | result = cs.take(); 25 | System.out.println(result.get()); 26 | es.shutdown(); 27 | } 28 | } 29 | 30 | class CalculateE implements Callable 31 | { 32 | final int lastIter; 33 | 34 | public CalculateE(int lastIter) 35 | { 36 | this.lastIter = lastIter; 37 | } 38 | 39 | @Override 40 | public BigDecimal call() 41 | { 42 | MathContext mc = new MathContext(100, RoundingMode.HALF_UP); 43 | BigDecimal result = BigDecimal.ZERO; 44 | for (int i = 0; i <= lastIter; i++) 45 | { 46 | BigDecimal factorial = factorial(new BigDecimal(i)); 47 | BigDecimal res = BigDecimal.ONE.divide(factorial, mc); 48 | result = result.add(res); 49 | } 50 | return result; 51 | } 52 | 53 | private BigDecimal factorial(BigDecimal n) 54 | { 55 | if (n.equals(BigDecimal.ZERO)) 56 | return BigDecimal.ONE; 57 | else 58 | return n.multiply(factorial(n.subtract(BigDecimal.ONE))); 59 | } 60 | } -------------------------------------------------------------------------------- /ch06/PhaserDemo/PhaserDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.Phaser; 6 | 7 | public class PhaserDemo 8 | { 9 | public static void main(String[] args) 10 | { 11 | List tasks = new ArrayList<>(); 12 | tasks.add(() -> System.out.printf("%s running at %d%n", 13 | Thread.currentThread().getName(), 14 | System.currentTimeMillis())); 15 | tasks.add(() -> System.out.printf("%s running at %d%n", 16 | Thread.currentThread().getName(), 17 | System.currentTimeMillis())); 18 | runTasks(tasks); 19 | } 20 | 21 | static void runTasks(List tasks) 22 | { 23 | final Phaser phaser = new Phaser(1); // "1" (register self) 24 | // create and start threads 25 | for (final Runnable task: tasks) 26 | { 27 | phaser.register(); 28 | Runnable r = () -> 29 | { 30 | try 31 | { 32 | Thread.sleep(50 + (int) (Math.random() * 300)); 33 | } 34 | catch (InterruptedException ie) 35 | { 36 | System.out.println("interrupted thread"); 37 | } 38 | phaser.arriveAndAwaitAdvance(); // await the ... 39 | // creation of ... 40 | // all tasks 41 | task.run(); 42 | }; 43 | Executors.newSingleThreadExecutor().execute(r); 44 | } 45 | // allow threads to start and deregister self 46 | phaser.arriveAndDeregister(); 47 | } 48 | } -------------------------------------------------------------------------------- /appa/ch06/PC/PC.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Semaphore; 2 | 3 | public class PC 4 | { 5 | public static void main(String[] args) 6 | { 7 | Shared s = new Shared(); 8 | Semaphore semCon = new Semaphore(0); 9 | Semaphore semPro = new Semaphore(1); 10 | new Producer(s, semPro, semCon).start(); 11 | new Consumer(s, semPro, semCon).start(); 12 | } 13 | } 14 | 15 | class Shared 16 | { 17 | private char c; 18 | 19 | void setSharedChar(char c) 20 | { 21 | this.c = c; 22 | } 23 | 24 | char getSharedChar() 25 | { 26 | return c; 27 | } 28 | } 29 | 30 | class Producer extends Thread 31 | { 32 | private final Shared s; 33 | private final Semaphore semPro, semCon; 34 | 35 | Producer(Shared s, Semaphore semPro, Semaphore semCon) 36 | { 37 | this.s = s; 38 | this.semPro = semPro; 39 | this.semCon = semCon; 40 | } 41 | 42 | @Override 43 | public void run() 44 | { 45 | for (char ch = 'A'; ch <= 'Z'; ch++) 46 | { 47 | try 48 | { 49 | semPro.acquire(); 50 | } 51 | catch (InterruptedException ie) 52 | { 53 | } 54 | s.setSharedChar(ch); 55 | System.out.println(ch + " produced by producer."); 56 | semCon.release(); 57 | } 58 | } 59 | } 60 | class Consumer extends Thread 61 | { 62 | private final Shared s; 63 | private final Semaphore semPro, semCon; 64 | 65 | Consumer(Shared s, Semaphore semPro, Semaphore semCon) 66 | { 67 | this.s = s; 68 | this.semPro = semPro; 69 | this.semCon = semCon; 70 | } 71 | 72 | @Override 73 | public void run() 74 | { 75 | char ch; 76 | do 77 | { 78 | try 79 | { 80 | semCon.acquire(); 81 | } 82 | catch (InterruptedException ie) 83 | { 84 | } 85 | ch = s.getSharedChar(); 86 | System.out.println(ch + " consumed by consumer."); 87 | semPro.release(); 88 | } 89 | while (ch != 'Z'); 90 | } 91 | } -------------------------------------------------------------------------------- /ch08/PC/PC.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.ArrayBlockingQueue; 2 | import java.util.concurrent.BlockingQueue; 3 | import java.util.concurrent.ExecutorService; 4 | import java.util.concurrent.Executors; 5 | 6 | public class PC 7 | { 8 | public static void main(String[] args) 9 | { 10 | final BlockingQueue bq; 11 | bq = new ArrayBlockingQueue(26); 12 | final ExecutorService executor = Executors.newFixedThreadPool(2); 13 | Runnable producer = () -> 14 | { 15 | for (char ch = 'A'; ch <= 'Z'; ch++) 16 | { 17 | try 18 | { 19 | bq.put(ch); 20 | System.out.printf("%c produced by " + 21 | "producer.%n", ch); 22 | } 23 | catch (InterruptedException ie) 24 | { 25 | } 26 | } 27 | }; 28 | executor.execute(producer); 29 | Runnable consumer = () -> 30 | { 31 | char ch = '\0'; 32 | do 33 | { 34 | try 35 | { 36 | ch = bq.take(); 37 | System.out.printf("%c consumed by " + 38 | "consumer.%n", ch); 39 | } 40 | catch (InterruptedException ie) 41 | { 42 | } 43 | } 44 | while (ch != 'Z'); 45 | executor.shutdownNow(); 46 | }; 47 | executor.execute(consumer); 48 | } 49 | } -------------------------------------------------------------------------------- /appa/ch03/Await/Await.java: -------------------------------------------------------------------------------- 1 | public class Await 2 | { 3 | static volatile int count; 4 | 5 | public static void main(String[] args) 6 | { 7 | Runnable r = () -> 8 | { 9 | Thread curThread = Thread.currentThread(); 10 | System.out.printf("%s has entered runnable and is " + 11 | "waiting%n", curThread.getName()); 12 | synchronized(Await.class) 13 | { 14 | count++; 15 | try 16 | { 17 | Thread.sleep(2000); 18 | while (count < 3) 19 | Await.class.wait(); 20 | } 21 | catch (InterruptedException ie) 22 | { 23 | } 24 | } 25 | System.out.printf("%s has woken up and is " + 26 | "terminating%n", 27 | curThread.getName()); 28 | }; 29 | Thread thdA = new Thread(r, "thdA"); 30 | Thread thdB = new Thread(r, "thdB"); 31 | Thread thdC = new Thread(r, "thdC"); 32 | thdA.start(); 33 | thdB.start(); 34 | thdC.start(); 35 | r = new Runnable() 36 | { 37 | @Override 38 | public void run() 39 | { 40 | try 41 | { 42 | while (count < 3) 43 | Thread.sleep(100); 44 | synchronized(Await.class) 45 | { 46 | Await.class.notifyAll(); 47 | } 48 | } 49 | catch (InterruptedException ie) 50 | { 51 | } 52 | } 53 | }; 54 | Thread thd = new Thread(r); 55 | thd.start(); 56 | } 57 | } -------------------------------------------------------------------------------- /appb/ViewPage/v1/ViewPage.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.Dimension; 3 | import java.awt.EventQueue; 4 | 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | import java.io.InputStream; 9 | import java.io.IOException; 10 | 11 | import java.net.URL; 12 | 13 | import javax.swing.JFrame; 14 | import javax.swing.JLabel; 15 | import javax.swing.JPanel; 16 | import javax.swing.JScrollPane; 17 | import javax.swing.JTextArea; 18 | import javax.swing.JTextField; 19 | 20 | public class ViewPage 21 | { 22 | public static void main(String[] args) 23 | { 24 | final JFrame frame = new JFrame("View Page"); 25 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 | JPanel panel = new JPanel(); 27 | panel.add(new JLabel("Enter URL")); 28 | final JTextField txtURL = new JTextField(40); 29 | panel.add(txtURL); 30 | frame.getContentPane().add(panel, BorderLayout.NORTH); 31 | final JTextArea txtHTML = new JTextArea(10, 40); 32 | frame.getContentPane().add(new JScrollPane (txtHTML), 33 | BorderLayout.CENTER); 34 | ActionListener al = (ae) -> 35 | { 36 | InputStream is = null; 37 | try 38 | { 39 | URL url = new URL(txtURL.getText()); 40 | is = url.openStream(); 41 | StringBuilder sb = new StringBuilder(); 42 | int b; 43 | while ((b = is.read()) != -1) 44 | sb.append((char) b); 45 | txtHTML.setText(sb.toString()); 46 | } 47 | catch (IOException ioe) 48 | { 49 | txtHTML.setText(ioe.getMessage()); 50 | } 51 | finally 52 | { 53 | txtHTML.setCaretPosition(0); 54 | if (is != null) 55 | try 56 | { 57 | is.close(); 58 | } 59 | catch (IOException ioe) 60 | { 61 | } 62 | } 63 | }; 64 | txtURL.addActionListener(al); 65 | frame.pack(); 66 | frame.setVisible(true); 67 | } 68 | } -------------------------------------------------------------------------------- /appb/ViewPage/v2/ViewPage.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.Dimension; 3 | import java.awt.EventQueue; 4 | 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | import java.io.InputStream; 9 | import java.io.IOException; 10 | 11 | import java.net.URL; 12 | 13 | import javax.swing.JFrame; 14 | import javax.swing.JLabel; 15 | import javax.swing.JPanel; 16 | import javax.swing.JScrollPane; 17 | import javax.swing.JTextArea; 18 | import javax.swing.JTextField; 19 | 20 | public class ViewPage 21 | { 22 | public static void main(String[] args) 23 | { 24 | Runnable r = () -> 25 | { 26 | final JFrame frame = new JFrame("View Page"); 27 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 28 | JPanel panel = new JPanel(); 29 | panel.add(new JLabel("Enter URL")); 30 | final JTextField txtURL = new JTextField(40); 31 | panel.add(txtURL); 32 | frame.getContentPane().add(panel, BorderLayout.NORTH); 33 | final JTextArea txtHTML = new JTextArea(10, 40); 34 | frame.getContentPane().add(new JScrollPane (txtHTML), 35 | BorderLayout.CENTER); 36 | ActionListener al = (ae) -> 37 | { 38 | InputStream is = null; 39 | try 40 | { 41 | URL url = new URL(txtURL.getText()); 42 | is = url.openStream(); 43 | StringBuilder sb = new StringBuilder(); 44 | int b; 45 | while ((b = is.read()) != -1) 46 | sb.append((char) b); 47 | txtHTML.setText(sb.toString()); 48 | } 49 | catch (IOException ioe) 50 | { 51 | txtHTML.setText(ioe.getMessage()); 52 | } 53 | finally 54 | { 55 | txtHTML.setCaretPosition(0); 56 | if (is != null) 57 | try 58 | { 59 | is.close(); 60 | } 61 | catch (IOException ioe) 62 | { 63 | } 64 | } 65 | }; 66 | txtURL.addActionListener(al); 67 | frame.pack(); 68 | frame.setVisible(true); 69 | }; 70 | EventQueue.invokeLater(r); 71 | } 72 | } -------------------------------------------------------------------------------- /ch06/CountDownLatchDemo/CountDownLatchDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.CountDownLatch; 2 | import java.util.concurrent.ExecutorService; 3 | import java.util.concurrent.Executors; 4 | 5 | public class CountDownLatchDemo 6 | { 7 | final static int NTHREADS = 3; 8 | 9 | public static void main(String[] args) 10 | { 11 | final CountDownLatch startSignal = new CountDownLatch(1); 12 | final CountDownLatch doneSignal = new CountDownLatch(NTHREADS); 13 | Runnable r = new Runnable() 14 | { 15 | @Override 16 | public void run() 17 | { 18 | try 19 | { 20 | report("entered run()"); 21 | startSignal.await(); // wait until told to ... 22 | report("doing work"); // ... proceed 23 | Thread.sleep((int) (Math.random() * 1000)); 24 | doneSignal.countDown(); // reduce count on which 25 | // main thread is ... 26 | } // waiting 27 | catch (InterruptedException ie) 28 | { 29 | System.err.println(ie); 30 | } 31 | } 32 | 33 | void report(String s) 34 | { 35 | System.out.println(System.currentTimeMillis() + 36 | ": " + Thread.currentThread() + 37 | ": " + s); 38 | } 39 | }; 40 | ExecutorService executor = Executors.newFixedThreadPool(NTHREADS); 41 | for (int i = 0; i < NTHREADS; i++) 42 | executor.execute(r); 43 | try 44 | { 45 | System.out.println("main thread doing something"); 46 | Thread.sleep(1000); // sleep for 1 second 47 | startSignal.countDown(); // let all threads proceed 48 | System.out.println("main thread doing something else"); 49 | doneSignal.await(); // wait for all threads to finish 50 | executor.shutdownNow(); 51 | } 52 | catch (InterruptedException ie) 53 | { 54 | System.err.println(ie); 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /ch05/CalculateE/CalculateE.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.math.MathContext; 3 | import java.math.RoundingMode; 4 | 5 | import java.util.concurrent.Callable; 6 | import java.util.concurrent.ExecutionException; 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.Executors; 9 | import java.util.concurrent.Future; 10 | 11 | public class CalculateE 12 | { 13 | final static int LASTITER = 17; 14 | 15 | public static void main(String[] args) 16 | { 17 | ExecutorService executor = Executors.newFixedThreadPool(1); 18 | Callable callable; 19 | callable = new Callable() 20 | { 21 | @Override 22 | public BigDecimal call() 23 | { 24 | MathContext mc = 25 | new MathContext(100, RoundingMode.HALF_UP); 26 | BigDecimal result = BigDecimal.ZERO; 27 | for (int i = 0; i <= LASTITER; i++) 28 | { 29 | BigDecimal factorial = 30 | factorial(new BigDecimal(i)); 31 | BigDecimal res = BigDecimal.ONE.divide(factorial, 32 | mc); 33 | result = result.add(res); 34 | } 35 | return result; 36 | } 37 | 38 | public BigDecimal factorial(BigDecimal n) 39 | { 40 | if (n.equals(BigDecimal.ZERO)) 41 | return BigDecimal.ONE; 42 | else 43 | return n.multiply(factorial(n. 44 | subtract(BigDecimal.ONE))); 45 | } 46 | }; 47 | Future taskFuture = executor.submit(callable); 48 | try 49 | { 50 | while (!taskFuture.isDone()) 51 | System.out.println("waiting"); 52 | System.out.println(taskFuture.get()); 53 | } 54 | catch(ExecutionException ee) 55 | { 56 | System.err.println("task threw an exception"); 57 | System.err.println(ee); 58 | } 59 | catch(InterruptedException ie) 60 | { 61 | System.err.println("interrupted while waiting"); 62 | } 63 | executor.shutdownNow(); 64 | } 65 | } -------------------------------------------------------------------------------- /ch02/DeadlockDemo/DeadlockDemo.java: -------------------------------------------------------------------------------- 1 | public class DeadlockDemo 2 | { 3 | private final Object lock1 = new Object(); 4 | private final Object lock2 = new Object(); 5 | 6 | public void instanceMethod1() 7 | { 8 | synchronized(lock1) 9 | { 10 | synchronized(lock2) 11 | { 12 | System.out.println("first thread in instanceMethod1"); 13 | // critical section guarded first by 14 | // lock1 and then by lock2 15 | } 16 | } 17 | } 18 | 19 | public void instanceMethod2() 20 | { 21 | synchronized(lock2) 22 | { 23 | synchronized(lock1) 24 | { 25 | System.out.println("second thread in instanceMethod2"); 26 | // critical section guarded first by 27 | // lock2 and then by lock1 28 | } 29 | } 30 | } 31 | 32 | public static void main(String[] args) 33 | { 34 | final DeadlockDemo dld = new DeadlockDemo(); 35 | Runnable r1 = new Runnable() 36 | { 37 | @Override 38 | public void run() 39 | { 40 | while(true) 41 | { 42 | dld.instanceMethod1(); 43 | try 44 | { 45 | Thread.sleep(50); 46 | } 47 | catch (InterruptedException ie) 48 | { 49 | } 50 | } 51 | } 52 | }; 53 | Thread thdA = new Thread(r1); 54 | Runnable r2 = new Runnable() 55 | { 56 | @Override 57 | public void run() 58 | { 59 | while(true) 60 | { 61 | dld.instanceMethod2(); 62 | try 63 | { 64 | Thread.sleep(50); 65 | } 66 | catch (InterruptedException ie) 67 | { 68 | } 69 | } 70 | } 71 | }; 72 | Thread thdB = new Thread(r2); 73 | thdA.start(); 74 | thdB.start(); 75 | } 76 | } -------------------------------------------------------------------------------- /ch08/MatMult/v2/MatMult.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | import java.util.concurrent.ForkJoinPool; 5 | import java.util.concurrent.RecursiveAction; 6 | 7 | public class MatMult extends RecursiveAction 8 | { 9 | private final Matrix a, b, c; 10 | private final int row; 11 | 12 | public MatMult(Matrix a, Matrix b, Matrix c) 13 | { 14 | this(a, b, c, -1); 15 | } 16 | 17 | public MatMult(Matrix a, Matrix b, Matrix c, int row) 18 | { 19 | if (a.getCols() != b.getRows()) 20 | throw new IllegalArgumentException("rows/columns mismatch"); 21 | this.a = a; 22 | this.b = b; 23 | this.c = c; 24 | this.row = row; 25 | } 26 | 27 | @Override 28 | public void compute() 29 | { 30 | if (row == -1) 31 | { 32 | List tasks = new ArrayList<>(); 33 | for (int row = 0; row < a.getRows(); row++) 34 | tasks.add(new MatMult(a, b, c, row)); 35 | invokeAll(tasks); 36 | } 37 | else 38 | multiplyRowByColumn(a, b, c, row); 39 | } 40 | 41 | public static void multiplyRowByColumn(Matrix a, Matrix b, Matrix c, 42 | int row) 43 | { 44 | for (int j = 0; j < b.getCols(); j++) 45 | for (int k = 0; k < a.getCols(); k++) 46 | c.setValue(row, j, c.getValue(row, j) + 47 | a.getValue(row, k) * b.getValue(k, j)); 48 | } 49 | 50 | public static void dump(Matrix m) 51 | { 52 | for (int i = 0; i < m.getRows(); i++) 53 | { 54 | for (int j = 0; j < m.getCols(); j++) 55 | System.out.print(m.getValue(i, j) + " "); 56 | System.out.println(); 57 | } 58 | System.out.println(); 59 | } 60 | 61 | public static void main(String[] args) 62 | { 63 | Matrix a = new Matrix(2, 3); 64 | a.setValue(0, 0, 1); // | 1 2 3 | 65 | a.setValue(0, 1, 2); // | 4 5 6 | 66 | a.setValue(0, 2, 3); 67 | a.setValue(1, 0, 4); 68 | a.setValue(1, 1, 5); 69 | a.setValue(1, 2, 6); 70 | dump(a); 71 | Matrix b = new Matrix(3, 2); 72 | b.setValue(0, 0, 7); // | 7 1 | 73 | b.setValue(1, 0, 8); // | 8 2 | 74 | b.setValue(2, 0, 9); // | 9 3 | 75 | b.setValue(0, 1, 1); 76 | b.setValue(1, 1, 2); 77 | b.setValue(2, 1, 3); 78 | dump(b); 79 | Matrix c = new Matrix(2, 2); 80 | ForkJoinPool pool = new ForkJoinPool(); 81 | pool.invoke(new MatMult(a, b, c)); 82 | dump(c); 83 | } 84 | } -------------------------------------------------------------------------------- /ch01/ThreadDemo/v3/ThreadDemo.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | 3 | public class ThreadDemo 4 | { 5 | // constant used in pi computation 6 | 7 | private static final BigDecimal FOUR = BigDecimal.valueOf(4); 8 | 9 | // rounding mode to use during pi computation 10 | 11 | private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN; 12 | 13 | private static BigDecimal result; 14 | 15 | public static void main(String[] args) 16 | { 17 | Runnable r = () -> 18 | { 19 | result = computePi(50000); 20 | }; 21 | Thread t = new Thread(r); 22 | t.start(); 23 | try 24 | { 25 | t.join(); 26 | } 27 | catch (InterruptedException ie) 28 | { 29 | // Should never arrive here because interrupt() is never 30 | // called. 31 | } 32 | System.out.println(result); 33 | } 34 | 35 | /* 36 | * Compute the value of pi to the specified number of digits after the 37 | * decimal point. The value is computed using Machin's formula: 38 | * 39 | * pi/4 = 4*arctan(1/5)-arctan(1/239) 40 | * 41 | * and a power series expansion of arctan(x) to sufficient precision. 42 | */ 43 | 44 | public static BigDecimal computePi(int digits) 45 | { 46 | int scale = digits + 5; 47 | BigDecimal arctan1_5 = arctan(5, scale); 48 | BigDecimal arctan1_239 = arctan(239, scale); 49 | BigDecimal pi = arctan1_5.multiply(FOUR). 50 | subtract(arctan1_239).multiply(FOUR); 51 | return pi.setScale(digits, BigDecimal.ROUND_HALF_UP); 52 | } 53 | 54 | /* 55 | * Compute the value, in radians, of the arctangent of the inverse of 56 | * the supplied integer to the specified number of digits after the 57 | * decimal point. The value is computed using the power series 58 | * expansion for the arc tangent: 59 | * 60 | * arctan(x) = x-(x^3)/3+(x^5)/5-(x^7)/7+(x^9)/9 ... 61 | */ 62 | 63 | public static BigDecimal arctan(int inverseX, int scale) 64 | { 65 | BigDecimal result, numer, term; 66 | BigDecimal invX = BigDecimal.valueOf(inverseX); 67 | BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX); 68 | numer = BigDecimal.ONE.divide(invX, scale, roundingMode); 69 | result = numer; 70 | int i = 1; 71 | do 72 | { 73 | numer = numer.divide(invX2, scale, roundingMode); 74 | int denom = 2 * i + 1; 75 | term = numer.divide(BigDecimal.valueOf(denom), scale, 76 | roundingMode); 77 | if ((i % 2) != 0) 78 | result = result.subtract(term); 79 | else 80 | result = result.add(term); 81 | i++; 82 | } 83 | while (term.compareTo(BigDecimal.ZERO) != 0); 84 | return result; 85 | } 86 | } -------------------------------------------------------------------------------- /ch02/JoinDemo/v2/JoinDemo.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | 3 | public class JoinDemo 4 | { 5 | // constant used in pi computation 6 | 7 | private static final BigDecimal FOUR = BigDecimal.valueOf(4); 8 | 9 | // rounding mode to use during pi computation 10 | 11 | private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN; 12 | 13 | private static volatile BigDecimal result; 14 | 15 | public static void main(String[] args) 16 | { 17 | Runnable r = () -> 18 | { 19 | result = computePi(50000); 20 | }; 21 | Thread t = new Thread(r); 22 | t.start(); 23 | try 24 | { 25 | t.join(); 26 | } 27 | catch (InterruptedException ie) 28 | { 29 | // Should never arrive here because interrupt() is never 30 | // called. 31 | } 32 | System.out.println(result); 33 | } 34 | 35 | /* 36 | * Compute the value of pi to the specified number of digits after the 37 | * decimal point. The value is computed using Machin's formula: 38 | * 39 | * pi/4 = 4*arctan(1/5)-arctan(1/239) 40 | * 41 | * and a power series expansion of arctan(x) to sufficient precision. 42 | */ 43 | 44 | public static BigDecimal computePi(int digits) 45 | { 46 | int scale = digits + 5; 47 | BigDecimal arctan1_5 = arctan(5, scale); 48 | BigDecimal arctan1_239 = arctan(239, scale); 49 | BigDecimal pi = arctan1_5.multiply(FOUR). 50 | subtract(arctan1_239).multiply(FOUR); 51 | return pi.setScale(digits, BigDecimal.ROUND_HALF_UP); 52 | } 53 | 54 | /* 55 | * Compute the value, in radians, of the arctangent of the inverse of 56 | * the supplied integer to the specified number of digits after the 57 | * decimal point. The value is computed using the power series 58 | * expansion for the arc tangent: 59 | * 60 | * arctan(x) = x-(x^3)/3+(x^5)/5-(x^7)/7+(x^9)/9 ... 61 | */ 62 | 63 | public static BigDecimal arctan(int inverseX, int scale) 64 | { 65 | BigDecimal result, numer, term; 66 | BigDecimal invX = BigDecimal.valueOf(inverseX); 67 | BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX); 68 | numer = BigDecimal.ONE.divide(invX, scale, roundingMode); 69 | result = numer; 70 | int i = 1; 71 | do 72 | { 73 | numer = numer.divide(invX2, scale, roundingMode); 74 | int denom = 2 * i + 1; 75 | term = numer.divide(BigDecimal.valueOf(denom), scale, 76 | roundingMode); 77 | if ((i % 2) != 0) 78 | result = result.subtract(term); 79 | else 80 | result = result.add(term); 81 | i++; 82 | } 83 | while (term.compareTo(BigDecimal.ZERO) != 0); 84 | return result; 85 | } 86 | } -------------------------------------------------------------------------------- /ch07/PC/PC.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.locks.Condition; 2 | import java.util.concurrent.locks.Lock; 3 | import java.util.concurrent.locks.ReentrantLock; 4 | 5 | public class PC 6 | { 7 | public static void main(String[] args) 8 | { 9 | Shared s = new Shared(); 10 | new Producer(s).start(); 11 | new Consumer(s).start(); 12 | } 13 | } 14 | 15 | class Shared 16 | { 17 | private char c; 18 | 19 | private volatile boolean available; 20 | 21 | private final Lock lock; 22 | 23 | private final Condition condition; 24 | 25 | Shared() 26 | { 27 | available = false; 28 | lock = new ReentrantLock(); 29 | condition = lock.newCondition(); 30 | } 31 | 32 | Lock getLock() 33 | { 34 | return lock; 35 | } 36 | 37 | char getSharedChar() 38 | { 39 | lock.lock(); 40 | try 41 | { 42 | while (!available) 43 | try 44 | { 45 | condition.await(); 46 | } 47 | catch (InterruptedException ie) 48 | { 49 | ie.printStackTrace(); 50 | } 51 | available = false; 52 | condition.signal(); 53 | } 54 | finally 55 | { 56 | lock.unlock(); 57 | return c; 58 | } 59 | } 60 | 61 | void setSharedChar(char c) 62 | { 63 | lock.lock(); 64 | try 65 | { 66 | while (available) 67 | try 68 | { 69 | condition.await(); 70 | } 71 | catch (InterruptedException ie) 72 | { 73 | ie.printStackTrace(); 74 | } 75 | this.c = c; 76 | available = true; 77 | condition.signal(); 78 | } 79 | finally 80 | { 81 | lock.unlock(); 82 | } 83 | } 84 | } 85 | class Producer extends Thread 86 | { 87 | private final Lock l; 88 | 89 | private final Shared s; 90 | 91 | Producer(Shared s) 92 | { 93 | this.s = s; 94 | l = s.getLock(); 95 | } 96 | 97 | @Override 98 | public void run() 99 | { 100 | for (char ch = 'A'; ch <= 'Z'; ch++) 101 | { 102 | l.lock(); 103 | s.setSharedChar(ch); 104 | System.out.println(ch + " produced by producer."); 105 | l.unlock(); 106 | } 107 | } 108 | } 109 | 110 | class Consumer extends Thread 111 | { 112 | private final Lock l; 113 | 114 | private final Shared s; 115 | 116 | Consumer(Shared s) 117 | { 118 | this.s = s; 119 | l = s.getLock(); 120 | } 121 | 122 | @Override 123 | public void run() 124 | { 125 | char ch; 126 | do 127 | { 128 | l.lock(); 129 | ch = s.getSharedChar(); 130 | System.out.println(ch + " consumed by consumer."); 131 | l.unlock(); 132 | } 133 | while (ch != 'Z'); 134 | } 135 | } -------------------------------------------------------------------------------- /ch02/JoinDemo/v1/JoinDemo.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | 3 | public class JoinDemo 4 | { 5 | // constant used in pi computation 6 | 7 | private static final BigDecimal FOUR = BigDecimal.valueOf(4); 8 | 9 | // rounding mode to use during pi computation 10 | 11 | private static final int roundingMode = BigDecimal.ROUND_HALF_EVEN; 12 | 13 | private static BigDecimal result; 14 | 15 | public static void main(String[] args) 16 | { 17 | Runnable r = () -> 18 | { 19 | synchronized(FOUR) 20 | { 21 | result = computePi(50000); 22 | } 23 | }; 24 | Thread t = new Thread(r); 25 | t.start(); 26 | try 27 | { 28 | t.join(); 29 | } 30 | catch (InterruptedException ie) 31 | { 32 | // Should never arrive here because interrupt() is never 33 | // called. 34 | } 35 | synchronized(FOUR) 36 | { 37 | System.out.println(result); 38 | } 39 | } 40 | 41 | /* 42 | * Compute the value of pi to the specified number of digits after the 43 | * decimal point. The value is computed using Machin's formula: 44 | * 45 | * pi/4 = 4*arctan(1/5)-arctan(1/239) 46 | * 47 | * and a power series expansion of arctan(x) to sufficient precision. 48 | */ 49 | 50 | public static BigDecimal computePi(int digits) 51 | { 52 | int scale = digits + 5; 53 | BigDecimal arctan1_5 = arctan(5, scale); 54 | BigDecimal arctan1_239 = arctan(239, scale); 55 | BigDecimal pi = arctan1_5.multiply(FOUR). 56 | subtract(arctan1_239).multiply(FOUR); 57 | return pi.setScale(digits, BigDecimal.ROUND_HALF_UP); 58 | } 59 | 60 | /* 61 | * Compute the value, in radians, of the arctangent of the inverse of 62 | * the supplied integer to the specified number of digits after the 63 | * decimal point. The value is computed using the power series 64 | * expansion for the arc tangent: 65 | * 66 | * arctan(x) = x-(x^3)/3+(x^5)/5-(x^7)/7+(x^9)/9 ... 67 | */ 68 | 69 | public static BigDecimal arctan(int inverseX, int scale) 70 | { 71 | BigDecimal result, numer, term; 72 | BigDecimal invX = BigDecimal.valueOf(inverseX); 73 | BigDecimal invX2 = BigDecimal.valueOf(inverseX * inverseX); 74 | numer = BigDecimal.ONE.divide(invX, scale, roundingMode); 75 | result = numer; 76 | int i = 1; 77 | do 78 | { 79 | numer = numer.divide(invX2, scale, roundingMode); 80 | int denom = 2 * i + 1; 81 | term = numer.divide(BigDecimal.valueOf(denom), scale, 82 | roundingMode); 83 | if ((i % 2) != 0) 84 | result = result.subtract(term); 85 | else 86 | result = result.add(term); 87 | i++; 88 | } 89 | while (term.compareTo(BigDecimal.ZERO) != 0); 90 | return result; 91 | } 92 | } -------------------------------------------------------------------------------- /ch06/SemaphoreDemo/SemaphoreDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.Executors; 2 | import java.util.concurrent.ExecutorService; 3 | import java.util.concurrent.Semaphore; 4 | 5 | public class SemaphoreDemo 6 | { 7 | public static void main(String[] args) 8 | { 9 | final Pool pool = new Pool(); 10 | Runnable r = new Runnable() 11 | { 12 | @Override 13 | public void run() 14 | { 15 | String name = Thread.currentThread().getName(); 16 | try 17 | { 18 | while (true) 19 | { 20 | String item; 21 | System.out.println(name + " acquiring " + 22 | (item = pool.getItem())); 23 | Thread.sleep(200 + 24 | (int) (Math.random() * 100)); 25 | System.out.println(name + " putting back " + 26 | item); 27 | pool.putItem(item); 28 | } 29 | } 30 | catch (InterruptedException ie) 31 | { 32 | System.out.println(name + "interrupted"); 33 | } 34 | } 35 | }; 36 | ExecutorService[] executors = 37 | new ExecutorService[Pool.MAX_AVAILABLE + 1]; 38 | for (int i = 0; i < executors.length; i++) 39 | { 40 | executors[i] = Executors.newSingleThreadExecutor(); 41 | executors[i].execute(r); 42 | } 43 | } 44 | } 45 | 46 | final class Pool 47 | { 48 | public static final int MAX_AVAILABLE = 10; 49 | 50 | private final Semaphore available = new Semaphore(MAX_AVAILABLE, true); 51 | 52 | private final String[] items; 53 | 54 | private final boolean[] used = new boolean[MAX_AVAILABLE]; 55 | 56 | Pool() 57 | { 58 | items = new String[MAX_AVAILABLE]; 59 | for (int i = 0; i < items.length; i++) 60 | items[i] = "I" + i; 61 | } 62 | 63 | String getItem() throws InterruptedException 64 | { 65 | available.acquire(); 66 | return getNextAvailableItem(); 67 | } 68 | 69 | void putItem(String item) 70 | { 71 | if (markAsUnused(item)) 72 | available.release(); 73 | } 74 | 75 | private synchronized String getNextAvailableItem() 76 | { 77 | for (int i = 0; i < MAX_AVAILABLE; ++i) 78 | { 79 | if (!used[i]) 80 | { 81 | used[i] = true; 82 | return items[i]; 83 | } 84 | } 85 | return null; // not reached 86 | } 87 | 88 | private synchronized boolean markAsUnused(String item) 89 | { 90 | for (int i = 0; i < MAX_AVAILABLE; ++i) 91 | { 92 | if (item == items[i]) 93 | { 94 | if (used[i]) 95 | { 96 | used[i] = false; 97 | return true; 98 | } 99 | else 100 | return false; 101 | } 102 | } 103 | return false; 104 | } 105 | } -------------------------------------------------------------------------------- /ch06/CyclicBarrierDemo/CyclicBarrierDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.concurrent.BrokenBarrierException; 2 | import java.util.concurrent.CyclicBarrier; 3 | 4 | public class CyclicBarrierDemo 5 | { 6 | public static void main(String[] args) 7 | { 8 | float[][] matrix = new float[3][3]; 9 | int counter = 0; 10 | for (int row = 0; row < matrix.length; row++) 11 | for (int col = 0; col < matrix[0].length; col++) 12 | matrix[row][col] = counter++; 13 | dump(matrix); 14 | System.out.println(); 15 | Solver solver = new Solver(matrix); 16 | System.out.println(); 17 | dump(matrix); 18 | } 19 | 20 | static void dump(float[][] matrix) 21 | { 22 | for (int row = 0; row < matrix.length; row++) 23 | { 24 | for (int col = 0; col < matrix[0].length; col++) 25 | System.out.print(matrix[row][col] + " "); 26 | System.out.println(); 27 | } 28 | } 29 | } 30 | 31 | class Solver 32 | { 33 | final int N; 34 | final float[][] data; 35 | final CyclicBarrier barrier; 36 | 37 | class Worker implements Runnable 38 | { 39 | int myRow; 40 | boolean done = false; 41 | 42 | Worker(int row) 43 | { 44 | myRow = row; 45 | } 46 | 47 | boolean done() 48 | { 49 | return done; 50 | } 51 | 52 | void processRow(int myRow) 53 | { 54 | System.out.println("Processing row: " + myRow); 55 | for (int i = 0; i < N; i++) 56 | data[myRow][i] *= 10; 57 | done = true; 58 | } 59 | 60 | @Override 61 | public void run() 62 | { 63 | while (!done()) 64 | { 65 | processRow(myRow); 66 | 67 | try 68 | { 69 | barrier.await(); 70 | } 71 | catch (InterruptedException ie) 72 | { 73 | return; 74 | } 75 | catch (BrokenBarrierException bbe) 76 | { 77 | return; 78 | } 79 | } 80 | } 81 | } 82 | 83 | public Solver(float[][] matrix) 84 | { 85 | data = matrix; 86 | N = matrix.length; 87 | barrier = new CyclicBarrier(N, 88 | new Runnable() 89 | { 90 | @Override 91 | public void run() 92 | { 93 | mergeRows(); 94 | } 95 | }); 96 | for (int i = 0; i < N; ++i) 97 | new Thread(new Worker(i)).start(); 98 | 99 | waitUntilDone(); 100 | } 101 | 102 | void mergeRows() 103 | { 104 | System.out.println("merging"); 105 | synchronized("abc") 106 | { 107 | "abc".notify(); 108 | } 109 | } 110 | 111 | void waitUntilDone() 112 | { 113 | synchronized("abc") 114 | { 115 | try 116 | { 117 | System.out.println("main thread waiting"); 118 | "abc".wait(); 119 | System.out.println("main thread notified"); 120 | } 121 | catch (InterruptedException ie) 122 | { 123 | System.out.println("main thread interrupted"); 124 | } 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /appb/SlideShow/ss/index: -------------------------------------------------------------------------------- 1 | sol,Sol 2 | sol2,Sol (Zoom) 3 | Sol3,Sol with Luna Eclipse 4 | mercury,Mercury 5 | mercury2,Mercury 6 | mercsurf1,Mercury Surface 7 | mercsurf2,Mercury Surface 8 | mercsurf3,Mickey Mouse on Mercury 9 | mercsurf4,Mercury Surface (Abedin Crater Interior) 10 | mercsurf5,Mercury Surface (North Pole) 11 | venus,Venus 12 | vensurf1,Venus Surface 13 | vensurf2,Venus Surface 14 | vensurf3,Venus Surface 15 | earthsys,Terran System 16 | earth,Earth 17 | earth2,Earth 18 | iss,International Space Station 19 | moon,Luna 20 | moonsurf1,Luna Surface 21 | moonsurf2,Luna Surface 22 | buzzaldrin,Buzz Aldrin on Luna 23 | earthrise,Earthrise 24 | earth3,Earth (1 Million Miles Away) 25 | em,Earth with Luna Transit 26 | marssys,Martian System 27 | mars,Mars 28 | marspole,Mars (South Pole) 29 | marsurf1,Martian Surface 30 | marsurf2,Martian Surface 31 | marsurf3,Martian Surface 32 | marsurf4,Martian Surface 33 | marsurf5,Martian Surface (Newton Crater) 34 | marsurf6,Martian Surface 35 | marsurf7,Martian Surface 36 | marsgold,Mars (Golden Ice Pits) 37 | phobos,Phobos 38 | deimos,Deimos 39 | emmars,Terran System (From Mars) 40 | gaspra,Gaspra 41 | ida,Ida 42 | cv,Ceres|Vesta 43 | ceres,Ceres 44 | ceres2,Ceres (Bright Spot) 45 | ceres3,Ceres (Bright Spot Zoom) 46 | ceres4,Ceres (Bright Spot More Zoom) 47 | ceres5,Ceres (North Pole) 48 | vesta,Vesta 49 | vesta-snowman,Vesta (Snowman Crater) 50 | vesta-southpole,Vesta (South Pole) 51 | jupsys,Jovian System 52 | jupiter,Jupiter 53 | jupclose,Jupiter Closeup 54 | metis,Metis 55 | adrastea,Adrastea 56 | amalthea,Amalthea 57 | thebe,Thebe 58 | io,Io 59 | europa,Europa 60 | ioeuropa,Io (Right) and Europa (Left) 61 | ganymede,Ganymede 62 | callisto,Callisto 63 | satsys,Saturnian System 64 | saturn,Saturn 65 | saturnnp,Saturn (North Pole) 66 | saturndm, Saturn with Dione and Mimas 67 | pan,Pan 68 | atlas,Atlas 69 | prometheus,Prometheus 70 | pandora,Pandora 71 | epimetheus,Epimetheus 72 | janus,Janus 73 | mimas,Mimas 74 | enceladus,Enceladus 75 | enceladus2,Enceladus Closeup 76 | enceladus3,Enceladus (South Corner) 77 | enceladus4,Enceladus (Southern Plumes) 78 | tethys,Tethys 79 | telesto,Telesto 80 | calypso,Calypso 81 | dione,Dione 82 | dione2,Dione (Zoom) 83 | dione3,Dione Transits Saturn 84 | dione4,Dione (Rings and Shadows) 85 | helene,Helene 86 | rhea,Rhea 87 | titan,Titan 88 | titsurf,Titan Surface 89 | hyperion,Hyperion 90 | hyperion2,Hyperion Closeup 91 | iapetus,Iapetus 92 | phoebe,Phoebe 93 | urasys,Uranian System 94 | uranus,Uranus 95 | uranus2,Uranus 96 | puck,Puck 97 | miranda,Miranda 98 | ariel,Ariel 99 | umbriel,Umbriel 100 | titania,Titania 101 | oberon,Oberon 102 | nepsys,Neptunian System 103 | neptune,Neptune 104 | ncc,Neptune (Cirrus Clouds) 105 | naiad,Naiad 106 | thalassa,Thalassa 107 | despina,Despina 108 | galatea,Galatea 109 | larissa,Larissa 110 | proteus,Proteus 111 | triton,Triton 112 | nereid,Nereid 113 | quaoar,Quaoar 114 | plutsys,Plutonian System 115 | plutchar,Pluto and Charon 116 | pluto,Pluto 117 | pluto2,Pluto (Zoom) 118 | plutsurf1,Pluto Surface 119 | plutsurf2,Pluto Surface (Ice Plains) 120 | plutsurf3,Pluto Surface (Flowing Ice) 121 | plutsurf4,Pluto Surface (Snakeskin Detail) 122 | charon,Charon 123 | charsurf,Charon Surface (Mountain in Moat) 124 | nix,Nix (False Color) 125 | hydra,Hydra 126 | plutsil,Pluto Silhouette 127 | makemake,MakeMake 128 | eris,Eris System 129 | sedna,Sedna 130 | kb,Kuiper Belt 131 | halley,Halley's Comet 132 | halnuc,Halley Nucleus 133 | 67p,Comet 67P 134 | oort,Oort Cloud 135 | edge,Solar System Edge 136 | -------------------------------------------------------------------------------- /ch07/Dictionary/Dictionary.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | import java.util.concurrent.Executors; 5 | import java.util.concurrent.ExecutorService; 6 | 7 | import java.util.concurrent.locks.Lock; 8 | import java.util.concurrent.locks.ReadWriteLock; 9 | import java.util.concurrent.locks.ReentrantReadWriteLock; 10 | 11 | public class Dictionary 12 | { 13 | public static void main(String[] args) 14 | { 15 | final String[] words = 16 | { 17 | "hypocalcemia", 18 | "prolixity", 19 | "assiduous", 20 | "indefatigable", 21 | "castellan" 22 | }; 23 | 24 | final String[] definitions = 25 | { 26 | "a deficiency of calcium in the blood", 27 | "unduly prolonged or drawn out", 28 | "showing great care, attention, and effort", 29 | "able to work or continue for a lengthy time without tiring", 30 | "the govenor or warden of a castle or fort" 31 | }; 32 | 33 | final Map dictionary = new HashMap(); 34 | 35 | ReadWriteLock rwl = new ReentrantReadWriteLock(true); 36 | final Lock rlock = rwl.readLock(); 37 | final Lock wlock = rwl.writeLock(); 38 | 39 | Runnable writer = () -> 40 | { 41 | for (int i = 0; i < words.length; i++) 42 | { 43 | wlock.lock(); 44 | try 45 | { 46 | dictionary.put(words[i], 47 | definitions[i]); 48 | System.out.println("writer storing " + 49 | words[i] + " entry"); 50 | } 51 | finally 52 | { 53 | wlock.unlock(); 54 | } 55 | 56 | try 57 | { 58 | Thread.sleep(1); 59 | } 60 | catch (InterruptedException ie) 61 | { 62 | System.err.println("writer " + 63 | "interrupted"); 64 | } 65 | } 66 | }; 67 | ExecutorService es = Executors.newFixedThreadPool(1); 68 | es.submit(writer); 69 | 70 | Runnable reader = () -> 71 | { 72 | while (true) 73 | { 74 | rlock.lock(); 75 | try 76 | { 77 | int i = (int) (Math.random() * 78 | words.length); 79 | System.out.println("reader accessing " + 80 | words[i] + ": " + 81 | dictionary.get(words[i]) 82 | + " entry"); 83 | } 84 | finally 85 | { 86 | rlock.unlock(); 87 | } 88 | } 89 | }; 90 | es = Executors.newFixedThreadPool(1); 91 | es.submit(reader); 92 | } 93 | } -------------------------------------------------------------------------------- /appb/ViewPage/v4/ViewPage.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.Dimension; 3 | import java.awt.EventQueue; 4 | 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | import java.io.InputStream; 9 | import java.io.IOException; 10 | 11 | import java.net.URL; 12 | 13 | import java.util.concurrent.ExecutionException; 14 | 15 | import javax.swing.JFrame; 16 | import javax.swing.JLabel; 17 | import javax.swing.JPanel; 18 | import javax.swing.JScrollPane; 19 | import javax.swing.JTextArea; 20 | import javax.swing.JTextField; 21 | import javax.swing.SwingWorker; 22 | 23 | public class ViewPage 24 | { 25 | public static void main(String[] args) 26 | { 27 | Runnable r = () -> 28 | { 29 | final JFrame frame = new JFrame("View Page"); 30 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 31 | JPanel panel = new JPanel(); 32 | panel.add(new JLabel("Enter URL")); 33 | final JTextField txtURL = new JTextField(40); 34 | panel.add(txtURL); 35 | frame.getContentPane().add(panel, BorderLayout.NORTH); 36 | final JTextArea txtHTML = new JTextArea(10, 40); 37 | frame.getContentPane().add(new JScrollPane (txtHTML), 38 | BorderLayout.CENTER); 39 | ActionListener al = (ae) -> 40 | { 41 | txtURL.setEnabled(false); 42 | class GetHTML extends SwingWorker 43 | { 44 | private final String url; 45 | 46 | GetHTML(String url) 47 | { 48 | this.url = url; 49 | } 50 | 51 | @Override 52 | public StringBuilder doInBackground() 53 | { 54 | StringBuilder sb = new StringBuilder(); 55 | InputStream is = null; 56 | try 57 | { 58 | URL url = new URL(this.url); 59 | is = url.openStream(); 60 | int b; 61 | while ((b = is.read()) != -1) 62 | sb.append((char) b); 63 | return sb; 64 | } 65 | catch (IOException ioe) 66 | { 67 | sb.setLength(0); 68 | sb.append(ioe.getMessage()); 69 | return sb; 70 | } 71 | finally 72 | { 73 | if (is != null) 74 | try 75 | { 76 | is.close(); 77 | } 78 | catch (IOException ioe) 79 | { 80 | } 81 | } 82 | } 83 | 84 | @Override 85 | public void done() 86 | { 87 | try 88 | { 89 | StringBuilder sb = get(); 90 | txtHTML.setText(sb.toString()); 91 | txtHTML.setCaretPosition(0); 92 | } 93 | catch (ExecutionException ee) 94 | { 95 | txtHTML.setText(ee.getMessage()); 96 | } 97 | catch (InterruptedException ie) 98 | { 99 | txtHTML.setText("Interrupted"); 100 | } 101 | txtURL.setEnabled(true); 102 | } 103 | } 104 | new GetHTML(txtURL.getText()).execute(); 105 | }; 106 | txtURL.addActionListener(al); 107 | frame.pack(); 108 | frame.setVisible(true); 109 | }; 110 | EventQueue.invokeLater(r); 111 | } 112 | } -------------------------------------------------------------------------------- /ch06/ExchangerDemo/ExchangerDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | import java.util.concurrent.Exchanger; 5 | 6 | public class ExchangerDemo 7 | { 8 | final static Exchanger exchanger = 9 | new Exchanger(); 10 | 11 | final static DataBuffer initialEmptyBuffer = new DataBuffer(); 12 | final static DataBuffer initialFullBuffer = new DataBuffer("I"); 13 | 14 | public static void main(String[] args) 15 | { 16 | class FillingLoop implements Runnable 17 | { 18 | int count = 0; 19 | 20 | @Override 21 | public void run() 22 | { 23 | DataBuffer currentBuffer = initialEmptyBuffer; 24 | try 25 | { 26 | while (true) 27 | { 28 | addToBuffer(currentBuffer); 29 | if (currentBuffer.isFull()) 30 | { 31 | System.out.println("filling thread wants to exchange"); 32 | currentBuffer = exchanger.exchange(currentBuffer); 33 | System.out.println("filling thread receives exchange"); 34 | } 35 | } 36 | } 37 | catch (InterruptedException ie) 38 | { 39 | System.out.println("filling thread interrupted"); 40 | } 41 | } 42 | 43 | void addToBuffer(DataBuffer buffer) 44 | { 45 | String item = "NI" + count++; 46 | System.out.println("Adding: " + item); 47 | buffer.add(item); 48 | } 49 | } 50 | 51 | class EmptyingLoop implements Runnable 52 | { 53 | @Override 54 | public void run() 55 | { 56 | DataBuffer currentBuffer = initialFullBuffer; 57 | try 58 | { 59 | while (true) 60 | { 61 | takeFromBuffer(currentBuffer); 62 | if (currentBuffer.isEmpty()) 63 | { 64 | System.out.println("emptying thread wants to " + 65 | "exchange"); 66 | currentBuffer = exchanger.exchange(currentBuffer); 67 | System.out.println("emptying thread receives " + 68 | "exchange"); 69 | } 70 | } 71 | } 72 | catch (InterruptedException ie) 73 | { 74 | System.out.println("emptying thread interrupted"); 75 | } 76 | } 77 | 78 | void takeFromBuffer(DataBuffer buffer) 79 | { 80 | System.out.println("taking: " + buffer.remove()); 81 | } 82 | } 83 | new Thread(new EmptyingLoop()).start(); 84 | new Thread(new FillingLoop()).start(); 85 | } 86 | } 87 | 88 | class DataBuffer 89 | { 90 | private final static int MAXITEMS = 10; 91 | 92 | private final List items = new ArrayList<>(); 93 | 94 | DataBuffer() 95 | { 96 | } 97 | 98 | DataBuffer(String prefix) 99 | { 100 | for (int i = 0; i < MAXITEMS; i++) 101 | { 102 | String item = prefix + i; 103 | System.out.printf("Adding %s%n", item); 104 | items.add(item); 105 | } 106 | } 107 | 108 | synchronized void add(String s) 109 | { 110 | if (!isFull()) 111 | items.add(s); 112 | } 113 | 114 | synchronized boolean isEmpty() 115 | { 116 | return items.size() == 0; 117 | } 118 | 119 | synchronized boolean isFull() 120 | { 121 | return items.size() == MAXITEMS; 122 | } 123 | 124 | synchronized String remove() 125 | { 126 | if (!isEmpty()) 127 | return items.remove(0); 128 | return null; 129 | } 130 | } -------------------------------------------------------------------------------- /appb/ViewPage/v3/ViewPage.java: -------------------------------------------------------------------------------- 1 | import java.awt.BorderLayout; 2 | import java.awt.Dimension; 3 | import java.awt.EventQueue; 4 | 5 | import java.awt.event.ActionEvent; 6 | import java.awt.event.ActionListener; 7 | 8 | import java.io.InputStream; 9 | import java.io.IOException; 10 | 11 | import java.lang.reflect.InvocationTargetException; 12 | 13 | import java.net.URL; 14 | 15 | import javax.swing.JFrame; 16 | import javax.swing.JLabel; 17 | import javax.swing.JPanel; 18 | import javax.swing.JScrollPane; 19 | import javax.swing.JTextArea; 20 | import javax.swing.JTextField; 21 | 22 | public class ViewPage 23 | { 24 | public static void main(String[] args) 25 | { 26 | Runnable r = () -> 27 | { 28 | final JFrame frame = new JFrame("View Page"); 29 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 30 | JPanel panel = new JPanel(); 31 | panel.add(new JLabel("Enter URL")); 32 | final JTextField txtURL = new JTextField(40); 33 | panel.add(txtURL); 34 | frame.getContentPane().add(panel, BorderLayout.NORTH); 35 | final JTextArea txtHTML = new JTextArea(10, 40); 36 | frame.getContentPane().add(new JScrollPane (txtHTML), 37 | BorderLayout.CENTER); 38 | ActionListener al = (ae) -> 39 | { 40 | txtURL.setEnabled(false); 41 | Runnable worker = () -> 42 | { 43 | InputStream is = null; 44 | try 45 | { 46 | URL url = new URL(txtURL.getText()); 47 | is = url.openStream(); 48 | final StringBuilder sb = new StringBuilder(); 49 | int b; 50 | while ((b = is.read()) != -1) 51 | sb.append((char) b); 52 | Runnable r1 = () -> 53 | { 54 | txtHTML.setText(sb.toString()); 55 | txtURL.setEnabled(true); 56 | }; 57 | try 58 | { 59 | EventQueue.invokeAndWait(r1); 60 | } 61 | catch (InterruptedException ie) 62 | { 63 | } 64 | catch (InvocationTargetException ite) 65 | { 66 | } 67 | } 68 | catch (final IOException ioe) 69 | { 70 | Runnable r1 = () -> 71 | { 72 | txtHTML.setText(ioe.getMessage()); 73 | txtURL.setEnabled(true); 74 | }; 75 | try 76 | { 77 | EventQueue.invokeAndWait(r1); 78 | } 79 | catch (InterruptedException ie) 80 | { 81 | } 82 | catch (InvocationTargetException ite) 83 | { 84 | } 85 | } 86 | finally 87 | { 88 | Runnable r1 = () -> 89 | { 90 | txtHTML.setCaretPosition(0); 91 | txtURL.setEnabled(true); 92 | }; 93 | try 94 | { 95 | EventQueue.invokeAndWait(r1); 96 | } 97 | catch (InterruptedException ie) 98 | { 99 | } 100 | catch (InvocationTargetException ite) 101 | { 102 | } 103 | if (is != null) 104 | try 105 | { 106 | is.close(); 107 | } 108 | catch (IOException ioe) 109 | { 110 | } 111 | } 112 | }; 113 | new Thread(worker).start(); 114 | }; 115 | txtURL.addActionListener(al); 116 | frame.pack(); 117 | frame.setVisible(true); 118 | }; 119 | EventQueue.invokeLater(r); 120 | } 121 | } -------------------------------------------------------------------------------- /appb/SlideShow/v1/SlideShow.java: -------------------------------------------------------------------------------- 1 | import java.awt.AlphaComposite; 2 | import java.awt.Color; 3 | import java.awt.Dimension; 4 | import java.awt.EventQueue; 5 | import java.awt.Font; 6 | import java.awt.FontMetrics; 7 | import java.awt.Graphics; 8 | import java.awt.Graphics2D; 9 | import java.awt.RenderingHints; 10 | 11 | import java.awt.event.ActionListener; 12 | import java.awt.event.WindowAdapter; 13 | import java.awt.event.WindowEvent; 14 | 15 | import java.awt.image.BufferedImage; 16 | 17 | import java.io.BufferedReader; 18 | import java.io.File; 19 | import java.io.FileReader; 20 | import java.io.IOException; 21 | 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | import javax.imageio.ImageIO; 26 | 27 | import javax.swing.JComponent; 28 | import javax.swing.JFrame; 29 | import javax.swing.Timer; 30 | 31 | class Projector 32 | { 33 | private volatile List slides; 34 | private Screen s; 35 | private Timer t; 36 | private volatile int slideIndexC, slideIndexN; 37 | private volatile float weight; 38 | 39 | Projector(List slides, Screen s) 40 | { 41 | this.slides = slides; 42 | this.s = s; 43 | t = new Timer(1500, null); 44 | t.setDelay(3000); 45 | slideIndexC = 0; 46 | slideIndexN = 1; 47 | } 48 | 49 | void start() 50 | { 51 | s.drawImage(Slide.blend(slides.get(0), null, 1.0f)); 52 | ActionListener al = (ae) -> 53 | { 54 | weight = 1.0f; 55 | Timer t2 = new Timer(0, null); 56 | t2.setDelay(10); 57 | ActionListener al2 = (ae2) -> 58 | { 59 | Slide slideC = slides.get(slideIndexC); 60 | Slide slideN = slides.get(slideIndexN); 61 | BufferedImage bi = Slide.blend(slideC, slideN, weight); 62 | s.drawImage(bi); 63 | weight -= 0.01f; 64 | if (weight <= 0.0f) 65 | { 66 | t2.stop(); 67 | slideIndexC = slideIndexN; 68 | slideIndexN = (slideIndexN + 1) % slides.size(); 69 | } 70 | }; 71 | t2.addActionListener(al2); 72 | t2.start(); 73 | }; 74 | t.addActionListener(al); 75 | t.start(); 76 | } 77 | 78 | void stop() 79 | { 80 | t.stop(); 81 | } 82 | } 83 | 84 | class Screen extends JComponent 85 | { 86 | private Dimension d; 87 | private BufferedImage bi; 88 | private String text; 89 | 90 | Screen(int width, int height) 91 | { 92 | d = new Dimension(width, height); 93 | } 94 | 95 | void drawImage(BufferedImage bi) 96 | { 97 | this.bi = bi; 98 | repaint(); 99 | } 100 | 101 | @Override 102 | public Dimension getPreferredSize() 103 | { 104 | return d; 105 | } 106 | 107 | @Override 108 | public void paint(Graphics g) 109 | { 110 | int w = getWidth(); 111 | int h = getHeight(); 112 | g.drawImage(bi, Slide.WIDTH <= w ? (w - Slide.WIDTH) / 2 : 0, 113 | Slide.HEIGHT <= h ? (h - Slide.HEIGHT) / 2 : 0, null); 114 | } 115 | } 116 | 117 | class Slide 118 | { 119 | static int WIDTH, HEIGHT; 120 | 121 | private static int TEXTBOX_WIDTH, TEXTBOX_HEIGHT, TEXTBOX_X, TEXTBOX_Y; 122 | 123 | private BufferedImage bi; 124 | private String text; 125 | private static Font font; 126 | 127 | private Slide(BufferedImage bi, String text) 128 | { 129 | this.bi = bi; 130 | this.text = text; 131 | font = new Font("Arial", Font.BOLD, 20); 132 | } 133 | 134 | static BufferedImage blend(Slide slide1, Slide slide2, float weight) 135 | { 136 | BufferedImage bi1 = slide1.getBufferedImage(); 137 | BufferedImage bi2 = (slide2 != null) 138 | ? slide2.getBufferedImage() 139 | : new BufferedImage(Slide.WIDTH, Slide.HEIGHT, 140 | BufferedImage.TYPE_INT_RGB); 141 | BufferedImage bi3 = new BufferedImage(Slide.WIDTH, Slide.HEIGHT, 142 | BufferedImage.TYPE_INT_RGB); 143 | Graphics2D g2d = bi3.createGraphics(); 144 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 145 | weight)); 146 | g2d.drawImage(bi1, 0, 0, null); 147 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 148 | 1.0f - weight)); 149 | g2d.drawImage(bi2, 0, 0, null); 150 | g2d.setColor(Color.BLACK); 151 | g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 152 | RenderingHints.VALUE_ANTIALIAS_ON); 153 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 154 | 0.5f)); 155 | 156 | g2d.fillRect(TEXTBOX_X, TEXTBOX_Y, TEXTBOX_WIDTH, TEXTBOX_HEIGHT); 157 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 158 | weight)); 159 | g2d.setColor(Color.WHITE); 160 | g2d.setFont(font); 161 | FontMetrics fm = g2d.getFontMetrics(); 162 | g2d.drawString(slide1.getText(), TEXTBOX_X + (TEXTBOX_WIDTH - 163 | fm.stringWidth(slide1.getText())) / 2, 164 | TEXTBOX_Y + TEXTBOX_HEIGHT / 2 + fm.getHeight() / 4); 165 | g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 166 | 1.0f - weight)); 167 | if (slide2 != null) 168 | g2d.drawString(slide2.getText(), TEXTBOX_X + (TEXTBOX_WIDTH - 169 | fm.stringWidth(slide2.getText())) / 2, TEXTBOX_Y + 170 | TEXTBOX_HEIGHT / 2 + fm.getHeight() / 4); 171 | g2d.dispose(); 172 | return bi3; 173 | } 174 | 175 | BufferedImage getBufferedImage() 176 | { 177 | return bi; 178 | } 179 | 180 | String getText() 181 | { 182 | return text; 183 | } 184 | 185 | static List loadSlides(String imagesPath) throws IOException 186 | { 187 | File imageFilesPath = new File(imagesPath); 188 | if (!imageFilesPath.isDirectory()) 189 | throw new IOException(imagesPath + " identifies a file"); 190 | List slides = new ArrayList<>(); 191 | try (FileReader fr = new FileReader(imagesPath + "/index"); 192 | BufferedReader br = new BufferedReader(fr)) 193 | { 194 | String line; 195 | while ((line = br.readLine()) != null) 196 | { 197 | String[] parts = line.split(","); 198 | File file = new File(imageFilesPath + "/" + parts[0] + ".jpg"); 199 | System.out.println(file); 200 | BufferedImage bi = ImageIO.read(file); 201 | if (WIDTH == 0) 202 | { 203 | WIDTH = bi.getWidth(); 204 | HEIGHT = bi.getHeight(); 205 | TEXTBOX_WIDTH = WIDTH / 2 + 10; 206 | TEXTBOX_HEIGHT = HEIGHT / 10; 207 | TEXTBOX_Y = HEIGHT - TEXTBOX_HEIGHT - 5; 208 | TEXTBOX_X = (WIDTH - TEXTBOX_WIDTH) / 2; 209 | } 210 | slides.add(new Slide(bi, parts[1])); 211 | } 212 | } 213 | if (slides.size() < 2) 214 | throw new IOException("at least one image must be loaded"); 215 | return slides; 216 | } 217 | } 218 | 219 | public class SlideShow 220 | { 221 | public static void main(String[] args) throws IOException 222 | { 223 | if (args.length != 1) 224 | { 225 | System.err.println("usage: java SlideShow ssdir"); 226 | return; 227 | } 228 | List slides = Slide.loadSlides(args[0]); 229 | final Screen screen = new Screen(Slide.WIDTH, Slide.HEIGHT); 230 | final Projector p = new Projector(slides, screen); 231 | Runnable r = () -> 232 | { 233 | final JFrame f = new JFrame("Slide Show"); 234 | WindowAdapter wa = new WindowAdapter() 235 | { 236 | @Override 237 | public void windowClosing(WindowEvent we) 238 | { 239 | p.stop(); 240 | f.dispose(); 241 | } 242 | }; 243 | f.addWindowListener(wa); 244 | f.setContentPane(screen); 245 | f.pack(); 246 | f.setVisible(true); 247 | p.start(); 248 | }; 249 | EventQueue.invokeLater(r); 250 | } 251 | } --------------------------------------------------------------------------------