26JavaCollectionsAndThreads

26JavaCollectionsAndThreads - CS108, Stanford Winter 2012...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #26 Winter 2012 Young Threads and Java Collections Thread support in Java’s built-in collections varies from class to class. In general we can place Java collections into three groups: Collections with No Thread Support Synchronized Collections Concurrent Collections Let’s take a close look at these groups. Collections with No Thread Support Unless a collection is explicitly documented as supporting threads, assume that no thread support is provided. This means that its internal data is not designed to handle changes from multiple threads. Using these collections in a multi-threaded program can easily result in corrupted data. Assume we have the following data structure defined: static List<String> testList = new ArrayList<String>(); Consider the following code: testList.add("Go Cardinal"); What can go wrong executing add in a multithreaded environment? We know adding an item into an ArrayList actually involves several steps. The computer must determine how many items are in the list, it must place the item into the array, and then it must increment the number of items in the list. In a multithreaded environment, any of these steps could end up interleaved with instructions from another thread, resulting in corrupted data. For example our thread might retrieve the number of items in the list, and then get swapped out for another thread which adds an item into the list. When our thread becomes active again it has the wrong number of items in the list, it proceeds to overwrite the data added by the other thread. Java collections without thread support may be used in a multithreaded environment, but extreme caution should be exercised. A collection which is only accessed from a single thread, for example, should not cause any problems. You can also write your own locking code to coordinate access to the collection from multiple threads.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Synchronized Collections Java provides a number of Synchronized Collections. These are collections in which each method is synchronized. As you may recall this means that any time a thread is running a method on an object, other threads attempting to perform an operation on the object will block until the first thread has completed the method. Going back to our previous example if we have a Synchronized List, then: testList.add("Go Cardinal"); causes no problems. If one thread is adding an item to the list, no other operation may be performed on the list, until the add operation has concluded. Available Synchronized Collections
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2012 for the course CS 108 taught by Professor Jimenez during the Winter '08 term at Stanford.

Page1 / 5

26JavaCollectionsAndThreads - CS108, Stanford Winter 2012...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online