HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Thread Synchronization in Java"
Vol 1, Issue 1, P. 14


Listing 1

routine vote(Candidate c) {
wait for one of the N voting booths
Booth b = claim booth
release booth b


Listing 2

public class Booth extends Object {
public synchronized void castVoteFor(Candidate c) {
// Register the vote with the Secretary of State...

Listing 3

public class SynchedFIFO extends Vector {
	public synchronized Object getNext() {
		while(0 == countElements())
		Object o = getElementAt(0);
		return o;
	public synchronized void put(Object o) {

Listing 4

public class Booth extends Object {
	SynchedFIFO _sf = null;
	public Booth(SynchedFIFO sf) {
		_sf = sf;
		// Register this Booth initially, since is free
		// when created.
public synchronized void castVoteFor(Candidate c) {
		// Register vote with the Secretary of State

Listing 5

public void vote(SynchedFIFO sf, Candidate c) {
// wait for and claim next free booth...
	Booth b = (Booth)sf.getNext();
	// cast vote, which automatically frees Booth
	// for use by another Voter when it completes.

Listing 6

public void MyUnsynchronizedMethod() {
	// bring a block of code under synchronization
	synchronized(this) {
	  // do some synchronized steps...
	  // This code is not synchronized...


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.