Home » Tag Archives: concurrent

Tag Archives: concurrent

The JavaFX Concurrent Framework

This is an article about the JavaFX Concurrent Framework API. Java 5 added a comprehensive concurrency framework to the Java programming language through the libraries in the java.util.concurrent package. The JavaFX Concurrency Framework is very small. It is built on top of the Java language Concurrency Framework keeping in mind that it will be used in a GUI environment.   ...

Read More »

Java ReentrantReadWriteLock Example

This is an example of how to make use of the ReentrantReadWriteLock class of Java. It is an implementation of ReadWriteLock, that also supports ReentrantLock functionality. The ReadWriteLock is a pair of associated Locks, one for read-only operations and one for writing. Whereas, the ReentrantLock is a reentrant mutual exclusion Lock with the same behavior as the implicit monitor lock accessed using synchronized methods ...

Read More »

Java AtomicMarkableReference Example

In this example we shall make use of AtomicMarkableReference class of Java. It is another class under the java.util.concurrent.atomic package, which provides lock-free and thread-safe programming solutions. In a recent example, we have seen how the AtomicReference class can be utilized to provide reference objects that may be atomically updated. Atomic actions are actions performed in an object only by ...

Read More »

Java AtomicInteger Example

This is an example of how to use the AtomicInteger class of Java. The java.util.concurrent.atomic package provides very useful classes that support lock-free and thread-safe programming on single variables. Among them, the AtomicInteger class is a wrapper class for an int value that allows it to be updated atomically. The class provides useful methods, some of which will be shown ...

Read More »

CountDownLatch example of a more general wait/notify mechanism

In this example we shall show you how to create a CountDownLatch of a more general wait/notify mechanism. We have implemented a method, that is testWaitNotify(). The steps of the method are described below: It creates an Object, to be the mutex. It creates a new Thread, that in its run() method acquires the object, using the synchronized() statement. Although ...

Read More »

Synchronous Queue example to execute commands

private BlockingQueue workQueue = new LinkedBlockingQueue(); private Map commandQueueMap = new ConcurrentHashMap(); public SynchronousQueue addCommand(Command command) { SynchronousQueue queue = new SynchronousQueue(); commandQueueMap.put(command, queue); workQueue.offer(command); return queue; } public Object call() throws Exception { try { Command command = workQueue.take(); Result result = command.execute(); SynchronousQueue queue = commandQueueMap.get(command); queue.offer(result); return null; } catch (InterruptedException e) { throw new WorkException(e); } ...

Read More »

Blocking Queue example of limited connection pool

private BlockingQueue<Connection> pool = new ArrayBlockingQueue<Connection>(10); private AtomicInteger connCount = new AtomicInteger(); public Connection getConnection() { Connection conn = pool.poll(5, TimeUnit.SECONDS); if (conn == null) { synchronized (connCount) { if (connCount.get() < 10) { conn = getNewConnection(); pool.offer(conn); connCount.incrementAndGet(); } } if (conn == null) { throw new ConnUnavailException(); } else { return conn; } } } Related Article: Java ...

Read More »

Blocking Queue example to execute commands

import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; private BlockingQueue<Command> workQueue = new LinkedBlockingQueue<Command>(); public void addCommand(Command command) { workQueue.offer(command); } public Object call() throws Exception { try { Command command = workQueue.take(); command.execute(); } catch (InterruptedException e) { throw new WorkException(e); } } Related Article: Java Concurrency Tutorial – Blocking Queues Reference: Java Concurrency Part 5 – Blocking Queues from our JCG partners ...

Read More »

Reentrant Lock example of a task runner

With this example we are going to demonstrate how to implement a ReentrantLock of a task runner. The ReentrantLock is a reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. The custom Class that have implemented is called TaskRunner and consists of the ...

Read More »

Reentrant ReadWriteLock example of value calculator

This is an example of how to use a ReentrantReadWriteLock of a value calculator. We have implemented a method that uses a ReadWriteLock and implements the calculate(int value), the getCalculatedValue() and the getValue() methods. In short the class is described below: It creates a ReentrantReadWriteLock. First it calls calculate(int value) with the int value that is to be calculated. The ...

Read More »