O'Reilly logo

Programmer's Guide to Java™ Certification, A: A Comprehensive Primer, Second Edition by Rolf W. Rasmussen, Khalid A. Mughal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

9. Threads

9.1
 // Filename: Counter.java /* Notice that the result of running this program may not be what you expect. Since both threads are working on full throttle it is possible that only one of the threads is granted CPU time. */ public class Counter implements Runnable { public static void main(String[] args) { Storage store = new Storage(); new Counter(store); new Printer(store); } Storage storage; Counter(Storage target) { storage = target; new Thread(this).start(); } public void run() { int i=0; while (true) { storage.setValue(i); i++; } } } class Printer implements Runnable { Storage storage; Printer(Storage source) { storage = source; new Thread(this).start(); } public void run() { while (true) { System.out.println(storage.getValue()); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required