Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To Cay and Gary: Concurrency Utilities

 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cay and Gary,

What have this actually done and enhanced for us in a multi-threaded environment?

Nick
 
Cay Horstmann
author
Ranch Hand
Posts: 172
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The concurrency utilities are actually the most important part of the Tiger multithreading support. In a previous posting I had a three-part strategy to access shared data from multiple threads:

1) Avoid the problem by using a thread-safe collection
2) Use synchronization (synchronized keyword or Lock/Condition)
3) Use volatile

The concurrency utilities provide the support for the first option.

For example, it is often possible to implement an algorithm so that some worker thread put their work on a queue, and other threads take the work off that queue. That way, objects are handed off safely, and the only shared object is the queue itself.

java.util.concurrent gives you threadsafe implementations of queues, maps, arrays, and priority queues that are a lot more sophisticated than what you learned in college. For example, according to the javadoc, LinkedQueue "employs an efficient "wait-free" algorithm based on one described in Simple, Fast, and Practical Non-Blocking and Blocking Concurrent Queue Algorithms by Maged M. Michael and Michael L. Scott."

In addition, java.util.concurrent gives you implementations of a thread pool, so you don't have to reinvent that wheel. There are a few other goodies--see Core Java vol. 2 ch. 1.

Cheers,

Cay
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone kindly please explain what is thread safe means ? thank you !
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is thread safe means

Thread safe means, when an object is being invoked by more than 1 object at the same time, the state of that object will be consistent to the same caller object.

Nick
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cay,

That is to say, Tiger has already provided a set of thread safe collections, or still, we need to do *something* to make sure the collection is thread safe?

Nick
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic