In this article, we will examine the subtleties of using newly introduced Lock classes in java.util.concurrent.locks package and their advantages over traditional "synchronized" blocks and methods.
sharadjava's blog
Java Concurrency Series- How to use Lock/RentrantLock in Java 5
Java Concurrency Series- How to use Future/FutureTask and Callable
In this article we will discuss how to use the new interface Callable introduced in java.concurrent.util in Java 5. Callable along with another interface Future allows us to obtain a return value from a method executed in a separate thread.
In the second example we will also examine the usage of FutureTask, an implementation of Future.
- 5 comments
- Read more
- 4779 reads
Java Concurrency Series - another Executor example
In an earlier article on Executor, we looked at how we can create use the executor to create thread pools to manage our multi-threaded program. We shall continue to explore additional features of this class.
Here we will extend the ThreadPooledExecutor class and provide our implementation to replace the empty implementation.
Java Concurrency Series - meaning of volatile variable
One of the least understood aspects of Java Thread model is the "volatile" keyword. The general misconception is that if you make a variable volatile you don't need to provide synchronized access to it.
Let's create a simple class with a integer marked as volatile. There are two methods which increment and decrement the variable in a for loop. To increase interleaving of the threads, we have a call to yield inside the for loop.
Code snippet 1
- public class VolatileExample {
- private volatile int volatileInt=0;
- /** unsynchronized volatile variable being incremented */
- 2 comments
- Read more
- 1262 reads
Java Concurrency Series - Simple Executor example
With Java 1.5 we have a new way of managing threads or threaded tasks. We no longer need to call start() and join()on our Thread object. The new concurrent package provides us with an elegant Executor class to manage our threads.
Let's go through a simple example given below. We have created a simple ThreadPool with default pool size of 2 and max size of 4. In the method createMultipleConsumerThreads(), we have created an instance of a Runnable task, Consumer and passed it on to the execute() method.
- Add new comment
- Read more
- 2757 reads
Java Concurrency Series with examples- Barrier
In this series we will discuss different concurrent structures provided in the new Java 5 Concurrent package.
We will start with a simple parallel processing concept called "barrier" that we have all studied and forgotten. By definition if there are set of independent tasks (T1..Tn) that need to fully completed to initiate another task (Tx) which is dependent on all these tasks (T1...Tn) we can synchronize them using a barrier.
- Add new comment
- Read more
- 1663 reads