Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

 

and many 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 »

Semaphores example limiting URL connections

In this example we shall show you how to use a Semaphore for limiting URL connections. We have implemented a class, ConnectionLimiter that uses a Semaphore and is described below: It creates a new Semaphore with a given number of permits. It implements a method, URLConnection acquire(URL url). The method acquires a permit from this semaphore, blocking until one is ...

Read More »

Exchanger example passing logs to a background logger

With this example we are going to demonstrate how to create an Exchanger passing logs to a background logger. We have implemented a class, BackgroundLogger that implements the Runnable interface. In short the class is described below: It consists of a static class LogEntry, that has three fields, a long time, an int value and a Stringbuilder, and a static class LogEntries, ...

Read More »

Store preferences to user space

This is an example of how to store Preferences to user space. The Preferences class allows applications to store and retrieve user and system preference and configuration data. Storing Preferences to user space implies that you should: Get the root preference node for the calling user, using userRoot() API method of Preferences. Get the named preference for the given pathname of the ...

Read More »

Read preferences from user space

In this example we shall show you how to read the Preferences from user space. To read the Preferences from user space one should perform the following steps: Get the root preference node for the calling user, using userRoot() API method of Preferences. Get the named preference for a given path name of the preference node, with node(String pathName) API ...

Read More »