Java concurrency package: Difference between revisions
imported>Michael T. Rakszawski m (Changed header) |
imported>Kevin Meredith |
||
Line 55: | Line 55: | ||
TBD | TBD | ||
===Executors=== | ===Executors=== | ||
Executors handle Runnable interfaces by providing high-level thread management, cradle-to-grave of a thread’s life. Runnable interfaces define a block of code that shall be run when a thread starts. Consider this example, | |||
// This class extends Thread | |||
class BasicThread1 extends Thread { | |||
// This method is called when the thread runs | |||
public void run() | |||
{ } | |||
} | |||
<ref>url =http://www.exampledepot.com/egs/java.lang/BasicThread.html, title = Creating a Thread </ref> | |||
A Java-focused website delivers the gist of Executors, “The new Executor framework solves all those [thread management] problems in a way that decouples task submission from the mechanics of how each task will be run, including the details of thread use, scheduling, etc”. <ref>url =http://www.exampledepot.com/egs/java.lang/BasicThread.html, title = Creating a Thread </ref> Without executors, a call to start a thread might look like this: | |||
new Thread (aRunnableObject).start (); | |||
Executors, which abstract the manual, non-trivial management of threads, would make this call to achieve the above result, | |||
Executor executor = some Executor factory method; | |||
exector.execute (aRunnable); | |||
In sum, the bottom line is that Executors make life easier for the concurrent Java programmer by dealing with the low-level details of thread management. | |||
(http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/package-summary.html) | |||
===Queues=== | ===Queues=== | ||
TBD | TBD |
Revision as of 20:02, 13 August 2010
To provide students with experience in collaboration, you are warmly invited to join in here, or to leave comments on the discussion page. The anticipated date of course completion is 13 August 2010. One month after that date at the latest, this notice shall be removed. Besides, many other Citizendium articles welcome your collaboration! |
The Java concurrency package is a library supporting threading and parallel programming in the Java programming language.
Introduction
As more and more computers are built with multi-core processors, concurrency has become an important topic in the field of computer science. Concurrency is "two or more threads running asynchronously, on one or more cores, usually in the same computer."[1] In contrast to linear programming, concurrent programming allows for the use of multiple processing resources to solve a problem, provided the software is built to take advantage of it.
Dangers of Concurrency
In concurrent programming, threads perform small packages of work. However, managing threads can be complicated, and if not managed correctly, can cause problems such as:
- Race conditions
- Deadlock
- Livelock
- Starvation
A race condition occurs when two threads try to access the same resource. This may cause an unexpected result.
Deadlock occurs when two or more threads are waiting for resources that are occupied by one another. This often causes computers to freeze.
Livelive is the condition when two or more threads are trying to avoid deadlock but prevent normal execution by doing so.
Starvation occurs when a thread is denied a resource.
Thread Safety
Programming languages are constantly being modified to support multithreaded programming and concurrency. The development of concurrency capabilities also comes with thread safety in order to avoid the common dangers. Thread safety generally takes two forms[2]:
- Lock-based synchronization
- Sophisticated data structures
Lock-based synchronization takes place when objects are limited to being modified by only one thread at a time. This prevents data from being corrupted by other threads that may want to access the resource at the same time.
Sophisticated data structures provide ways for programmers to handle threads without managing every minor detail of their communication and activities. These may include variations of queues, maps, or other data structures that are built specifically to handle concurrency.
Brief History of Concurrent Programming
Historically, concurrency has been implemented via the operating system.[3] It was only understood by experienced systems programmers who had to made system calls to manage threads. As the years passed, new programming languages appeared that provided built-in support for concurrency. Java is one example.
Introduction of Java
Java was built to support concurrency in that all Java applications contain threads of execution.[3] From its introduction, Java supported primitive multithreaded programming via the Runnable interface, which is part of the java.lang package. The Runnable interface provides the most basic functionality for multithreading.[3] Objects that are Runnable can be executed by a thread.
However, the Runnable interface had limited concurrency capabilities. Simple locks could be put in place on objects. Programmers had to use methods such as wait(), notify(), and notifyAll() to control locking and communication between threads. The Java developers noticed this and made significant improvements to the concurrency support with the introduction of J2SE 5.0.
Improved Concurrency Support in Java 5
J2SE 5.0 was released on September 29, 2004.[4] J2SE 5.0 introduced the java.util.concurrent package, which was designed for concurrency.[2] The package includes a number of standardized extensible frameworks. It contains five main components:
- Executors
- Queues
- Timing
- Synchronizers
- Concurrent Collections
Using the java.util.concurrent package, programmers can take advantage of sophisticated data structures that are built specifically for concurrent programming. A comprehensive list of the objects and methods available in the java.util.concurrent package can be found on the Java API.
Five Main Components
TBD
Executors
Executors handle Runnable interfaces by providing high-level thread management, cradle-to-grave of a thread’s life. Runnable interfaces define a block of code that shall be run when a thread starts. Consider this example,
// This class extends Thread
class BasicThread1 extends Thread {
// This method is called when the thread runs
public void run()
{ }
} [5]
A Java-focused website delivers the gist of Executors, “The new Executor framework solves all those [thread management] problems in a way that decouples task submission from the mechanics of how each task will be run, including the details of thread use, scheduling, etc”. [6] Without executors, a call to start a thread might look like this: new Thread (aRunnableObject).start (); Executors, which abstract the manual, non-trivial management of threads, would make this call to achieve the above result, Executor executor = some Executor factory method; exector.execute (aRunnable); In sum, the bottom line is that Executors make life easier for the concurrent Java programmer by dealing with the low-level details of thread management. (http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/package-summary.html)
Queues
TBD
Timing
TBD
Synchronizers
Mutilthread Synchronization before Java 5
Before Java 5, multithreads are supported using the lock mechanism for synchronization. Locks are implemented in Synchronized method. This mechanism “ensures that only one Java thread can execute an object's synchronized methods at a time” and “also allows threads to wait for resources to become available, and allows the thread that makes resources available to notify other threads that are waiting for the resources”. [7]. When the synchronized keyword is used, the thread which invokes the synchronized method must obtain a lock for the object which makes this thread the lock holder. The rule of thumb of synchronized method is that only one thread can hold this lock at a time.
Three most commonly used methods including, wait(), notify(), and notifyAll() are used for resource communication between threads.
“The wait() method can only be invoked by the object's lock holder. It causes current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object” [8].
The notify() method wakes up one thread and only the notified thread can go ahead and do something. If there are more than one thread waiting on this object’s waiting queue, one of them is selected to be woke up. notify() wakes up the first thread in the waiting queue.
The notifyAll() method wakes up all threads in the wait set. notifyAll() is normally used when there are many threads to wake up simultaneously. Which thread gets the right to go ahead to execute depends upon thread property such as their priority.
One of the biggest issues of synchronized method is that it is an “all-or-nothing thing” [9]. “Once a thread attempts to enter a synchronized block, it will hang until the lock is available.” [9] which causes low performance because all other threads that need the same object have to wait. Another issue is that missing appropriate notifications such as notify() or notifyAll() while programming probably results in deadlock. [10]
Java 5: Synchronizers
In Java 5, the Java concurrent package provides four new classes including: semaphore, countdownlatch, cyclickbarrier, and exchanger, used for data synchronization [11].
Semaphore is a counting signal. It maintains a set of permits. The “parking garage” can be a good analogy as an example to explain it. We can think of that the permits in semaphore equals to the capability in a garage. If the permits that have been issued reach the maximum garage capacity, the garage is full. No parking permit can be issued. The garage will have some space for other cars when some permits are returned.
“CountDownLatch is a synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. It is initialized with a given count” [12]. Something here that needs to be mentioned that “CountDownLatch is a one-shot phenomenon” [12] meaning that the counter cannot be reused. If you need a counter that can be reset, consider using the CyclicBarrier method stating below.
CyclicBarriers is an aid “which allows a set of threads to wait for each other to reach a common barrier point” [12]. CyclicBarriers is useful when a fixed sized group of thread is occasionally but must wait for each other in a program. “The barrier is called cyclic because it can be re-used after the waiting threads are released” [12].
Exchanger is actually what the word looks like. “It is a synchronization point where two threads can exchange objects. Each thread presents some object on entry to the exchange method and receives the object presented by the other thread on return” [12].
Concurrent Collections
As it is mentioned in the beginning of synchronizer, the lock mechanism is implemented in “synchronized” classes before Java 5 and “synchronized” method will lock the object that’s been accessed by a thread. When an object is locked by one thread, other threads which need to access the same object are going to wait until the object is available. Also, only the resources in the object are protected. Other resources outside the object are not.
"Synchronized classes can be useful when you need to prevent all access to a collection through a single lock, at the expense of poorer scalability”[11].
Java realized this issue and therefore enhanced it by supplying some more collection implementations such as ConcurrentHashMap, CopyOnWriteArrayList , and CopyOnWriteArraySet. Here only basic concepts will be introduced here due to that this component is a very advanced one in java concurrent package. For more information, please refer to J2SE 5.0 official website Package java.util.concurrenct.
ConcurrentHashMap: “It synchronizes different segments in a hash table, not the whole object. Therefore, other threads can still access other segments that are not in synchronization” [13]. So other threads don’t have to wait. This provides the thread safety and also improves balances of the performance.
CopyOnWriteArrayList: “It uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible” [13]. This means no modification will be allowed when using CopyOnWriteArrayList. Therefore this operation is immutable and also guarantees thread-safety.
CopyOnWriteArraySet: It’s a data structure to protect the data during traversal for modification of the Array. With CopyOnWriteArraySet, any changes in the data structure during traversal results in a copy being made for the modification. However, there is something we have to keep in mind. This is used assuming in that there won’t be too many changes being made. If a lot of possible modifications need to be performed, we are talking a lot of copies as well which could be bad.
Conclusion
Capability
Java 5 still supports the standard concurrent unities which are contained in the previous version and has introduced the new java.util.concurrent package to make the life of concurrent development in Java easier by providing those high-quality implementations for the data synchronization mechanisms [13].
Advantages
The benefits of using java.util.concurrent package for developers include [13]
Unsolved Issue
The Java concurrency package is not a fix-all. Many of the common concurrent programming issues remain unresolved. The Java concurrency package still does not ensure there will not be any deadlock or CPU starvation in an application. It is the responsibility of developers to take appropriate actions to handle concurrency and data synchronization in their applications.
Related Articles
External Links
References
- ↑ A Concise Guide to Concurrent Programming. Retrieved on 2010-08-13.
- ↑ 2.0 2.1 The Java Programming Language, Fourth Edition, Arnold, Addison-Wesley © 2006 Sun Microsystems, Inc.
- ↑ 3.0 3.1 3.2 Java: How to Program, Sixth Edition, Deitel, Pearson © 2005
- ↑ J2SE Code Names. Retrieved on 2010-08-13.
- ↑ url =http://www.exampledepot.com/egs/java.lang/BasicThread.html, title = Creating a Thread
- ↑ url =http://www.exampledepot.com/egs/java.lang/BasicThread.html, title = Creating a Thread
- ↑ Gary Shute. Java Synchronization. Retrieved on 2010-08-12.
- ↑ J2SE API v1.4.2. Retrieved on 2010-08-12.
- ↑ 9.0 9.1 Unknown. Problems with Java 1.4 synchronization model. Retrieved on 2010-08-12.
- ↑ V.K., Garg (September 2005). "A Critique of Java for Concurrent Programming". IEEE Computer Society 6 (9). Retrieved on 2010-08-12. [e]
- ↑ 11.0 11.1 J2SE API v5.0. Retrieved on 2010-08-12.
- ↑ 12.0 12.1 12.2 12.3 12.4 public class: CountDownLatch. Retrieved on 2010-08-12.
- ↑ 13.0 13.1 13.2 13.3
Qusay H. Mahmoud. Concurrent Programming with J2SE 5.0. Retrieved on 2010-08-12.
Cite error: Invalid
<ref>
tag; name "Concurrent Programming with J2SE 5.0" defined multiple times with different content Cite error: Invalid<ref>
tag; name "Concurrent Programming with J2SE 5.0" defined multiple times with different content