Chapter 6. Concurrency

Table of Contents

Read/Write Locks
ThreadSafeList
The Swing Event Dispatch Thread
Multithreading our IssuesBrowser
So What?

One great feature of Glazed Lists is that data can be updated in the background while a user interacts with your user interface. Glazed Lists is designed to be used with many threads, each reading or writing their list of choice. In this section we show how to use Glazed Lists concurrently.

Read/Write Locks

Every EventList has a method getReadWriteLock() that can be used to provide concurrent access to the list. Read/Write locks are designed to allow access for either multiple readers or a single writer. All locks in Glazed Lists are reentrant so that a single thread can lock() multiple times.

To read from a list that is being read or written by a different thread:

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

To write from a list that is being read or written by a different thread:

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