Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Using threads with collections, Part 2

New design issues arise when making collections larger

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
This column is a continuation of the discussion I started in the March Java In Depth column. In that first part I discussed building a class that implements the collection abstraction called SynchroList. In this column I'll update that class so that I can subset the collection. Then we'll go through the issues that arise when multiple threads use a subsetted collection. Finally, I'll show you an applet I wrote that helps demonstrate what is going on inside the collection when multiple threads are accessing it.

Collections that support subsets

The class that I created in part 1 of this series is called a SynchroList, and it is an ordered collection. Being ordered means that the objects within the collection have some notion of order associated with them. For example, words may be sorted into alphabetical order, numbers into numerical order, and so on. In the case of my class, the ordering is determined by a helper class called a Comparator. This ordering is similar to the way in which collections are set up in the 1.2 release of Java.

Once a collection is ordered, a user of the collection can specify a subset of elements in terms of example objects. While this may be confusing at first, it turns out to be a pretty simple concept.

Consider a collection of words (which for the purpose of this example will consist of an arbitrary number of characters and letters, tokens, not necessarily English words) that are ordered alphabetically using the convention that letters that appear earlier in the alphabet are considered to have a value less than letters that appear later in the alphabet. Within the collection, you may wish to select all tokens that start with the letter a. You make your selections from the list by comparing all of the tokens in the list with the tokens "a" and "b." If the list element is greater than or equal to the first token, that element is included in the subset as long as it is also less than the second token. The tokens selected become the subset. That subset can be expressed as ("a," "b"]. The difference between the ( symbol and the ] symbol is that a parenthesis implies greater than or equal to, while the bracket implies less than. The set is said to be inclusive of "a" and exclusive of "b."

To implement subsets in the SynchroList class I changed the inner class LinkEnumerator. Why? The original LinkEnumerator (for a review, see the original source in part 1 of this series) had only the null constructor. This set an internal index variable named current to point to the head of the sorted list of Link objects. In the new version of this class we add two constructors for returning subsets of the list. Further, we've added a pointer to the last object to be returned, named last, that tells the class when it has reached the end of the list. The original constructor was modified as follows:

   
LinkEnumerator( ) { current = head; last = null }


Next we've added two additional constructors to return subsets of the list. The first returns the subset from and object referenced by the variable from to the end of the collection. This constructor is shown next:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Comparator interface http://www.javaworld.com/jw-06-1998/indepth/Comparator.java
  • The example comparator for integers http://www.javaworld.com/jw-06-1998/indepth/IntCompare.java
  • The test program for the SynchroList class http://www.javaworld.com/jw-06-1998/indepth/SynchroTest.java
  • Subsetted version http://www.javaworld.com/jw-06-1998/indepth/SynchroList.java
  • Subsetted and threaded version with some methods to support the applet http://www.javaworld.com/jw-06-1998/indepth/SynchroList.java
  • Applet base class http://www.javaworld.com/jw-06-1998/indepth/Synchro.java
  • Helper class for the applet http://www.javaworld.com/jw-06-1998/indepth/State.java
  • Here are the sources, in ZIP format, for collections that support subsets http://www.javaworld.com/jw-06-1998/indepth/indepth1.java
  • Here are the sources in ZIP format, for adding threads to the picture http://www.javaworld.com/jw-06-1998/indepth/indepth2.java
  • "Using threads with collections, Part 1"
    Threads add a new degree of complexity to otherwise straightforward design issues. http://www.javaworld.com/javaworld/jw-03-1998/jw-03-indepth.html
  • "An in-depth look at Java's character type"
    Eight (bits) is not enough -- Java's character type adds another eight. http://www.javaworld.com/javaworld/jw-01-1998/jw-01-indepth.html
  • "A first look at Borland's JBuilder IDE" http://www.javaworld.com/jw-11-1997/jw-11-indepth.html
  • "A look at inner classes"
    Reduce class clutter in your Java designsUse inner classes. http://www.javaworld.com/javaworld/jw-10-1997/jw-10-indepth.html
  • "Take an in-depth look at the Java Reflection API"
    Learn about the new Java 1.1 tools for finding out information about classes. http://www.javaworld.com/javaworld/jw-09-1997/jw-09-indepth.html
  • "Take a look inside Java classes"
    Learn to deduce properties of a Java class from inside a Java program. http://www.javaworld.com/javaworld/jw-08-1997/jw-08-indepth.html
  • "Build an interpreter in Java -- Implement the execution engine"
    Here's how to take the interpreter classes and run with them. http://www.javaworld.com/javaworld/jw-07-1997/jw-07-indepth.html
  • "How to build an interpreter in Java, Part 2The structure"
    The trick to assembling the foundation classes for a simple interpreter. http://www.javaworld.com/javaworld/jw-06-1997/jw-06-indepth.html
  • "How to build an interpreter in Java, Part 1The BASICs"
    For complex applications requiring a scripting language, Java can be used to implement the interpreter, adding scripting abilities to any Java app. http://www.javaworld.com/javaworld/jw-05-1997/jw-05-indepth.html
  • "Lexical analysis, Part 2Build an application"
    How to use the StreamTokenizer object to implement an interactive calculator. http://www.javaworld.com/javaworld/jw-02-1997/jw-02-indepth.html
  • "Lexical analysis and JavaPart 1"
    Learn how to convert human-readable text into machine-readable data using the StringTokenizer and StreamTokenizer classes. http://www.javaworld.com/javaworld/jw-01-1997/jw-01-indepth.html
  • "Code reuse and object-oriented systems"
    Use a helper class to enforce dynamic behavior. http://www.javaworld.com/javaworld/jw-12-1996/jw-12-indepth.html
  • "Container support for objects in Java 1.0.2"
    Organizing objects is easy when you put them into containers. This article walks you through the design and implementation of a container. http://www.javaworld.com/javaworld/jw-11-1996/jw-11-indepth.html
  • "The basics of Java class loaders"
    The fundamentals of this key component of the Java architecture. http://www.javaworld.com/javaworld/jw-10-1996/jw-10-indepth.html
  • "Not using garbage collection"
    Minimize heap thrashing in your Java programs. http://www.javaworld.com/javaworld/jw-09-1996/jw-09-indepth.html
  • "Threads and applets and visual controls"
    This final part of the series explores reading multiple data channels. http://www.javaworld.com/javaworld/jw-07-1996/jw-07-mcmanis.html
  • "Using communication channels in applets, Part 3"
    Develop Visual Basic-style techniques to applet design -- and convert temperatures in the process. http://www.javaworld.com/javaworld/jw-06-1996/jw-06-mcmanis.html
  • "Synchronizing threads in Java, Part 2"
    Learn how to write a data channel class, and then create a simple example application that illustrates a real-world implementation of the class. http://www.javaworld.com/javaworld/jw-05-1996/jw-05-mcmanis.html
  • "Synchronizing threads in Java"
    Former Java team developer Chuck McManis walks you through a simple example illustrating how to synchronize threads to assure reliable and predictable applet behavior. http://www.javaworld.com/javaworld/jw-04-1996/jw-04-synch.html