Chapter 6. Concurrency

Table of Contents

Read/Write Locks
GlazedLists.threadSafeList
The Swing Event Dispatch Thread
Multithreading our IssuesBrowser
So What?

Concurrency support is build right into the central interface of Glazed Lists, EventList. This may seem like mixing unrelated concerns, but the advantages are worth it:

Note

If your EventLists are used by only one thread, you don't need to worry about locking.

Read/Write Locks

Every EventList has a method getReadWriteLock() that should be used for threadsafe access. Read/Write locks are designed to allow access by multiple readers or a single writer. The locks in Glazed Lists are reentrant which allows you to lock() multiple times before you unlock().

To read from an EventList that is shared with another thread:

  EventList myList = ...
  myList.getReadWriteLock().readLock().lock();
  try {
    // perform read operations like myList.size() and myList.get()
  } finally {
    myList.getReadWriteLock().readLock().unlock();
  }

To write to a shared EventList:

  EventList myList = ...
  myList.getReadWriteLock().writeLock().lock();
  try {
    // perform write operations like myList.set() and myList.clear()
  } finally {
    myList.getReadWriteLock().writeLock().unlock();
  }