Home » Core Java » threads (page 2)

threads

Worker coordination using WorkQueue

In this example we shall show you how to achieve a coordination between two threads that try to get the same objects. The steps of the example are described below: We have created a class, WorkerQueue, that has a LinkedList of Objects. In its synchronized void addWork(Object o) method it appends the object to the end of the list, with ...

Read More »

ThreadLocal example

With this example we are going to demonstrate how to create a ThreadLocal. The ThreadLocal class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread. In short, to create a ThreadLocal you ...

Read More »

Thread communication using Queue example

This is an example of how to achieve a queue communication between two Threads. The example is described in short: We have created a class, PrepProduct that implements the Runnable and has a BlockingQueue of Strings. It overrides the run() API method of the Runnable where it puts two elements to the BlockingQueue, waiting if necessary for space to become available, with ...

Read More »

Find rhyming words using QuickSort

In this example we shall show you how to find rhyming words using the QuickSort algorithm. We have created two classes, SortThread and ReverseThread that both extend the Thread and override the run() method of Thread. The two threads and the example are described in short: In their run() methods the SortThread calls its private quicksort(String[] str, int low0, int high0) method that ...

Read More »

Thread performance on Atomic type

With this example we are going to demonstrate how to check a thread performance on atomic type. We are testing two runnable objects, one that uses an AtomicInteger and another one that uses a synchronized method. The example is described in short: We have created a class, AtomicRunnable that implements the Runnable, uses an AtomicInteger and in its overriden run() ...

Read More »

Synchronized/Unsynchronized collection performance test

This is an example of how to test the performance of synchronized and unsynchronized Collection. The test is described below: We have a static method, performTest(List l) that adds a new Integer to the specified list for a specified int number of loops. We have also created a class, Timestamp, that in its start() and stop() methods gets the System.nanoTime() ...

Read More »

Safe collection iteration example

In this example we shall show you how to obtain a safe Collection iteration. We are using the List implementation of Collection, but the Collections API provides methods for such operations in other Collection implementations too, such as Map and Set. To obtain a safe Collection iteration one should perform the following steps: Call synchronizedList(List list) API method of Collections ...

Read More »

Vector copy example

With this example we are going to demonstrate how to get a copy of a Vector. We are using the synchronized statement in order to take a safe copy of the Vector’s elements. In short, to get a copy of a Vector you should: Create a new Vector. Populate the vector with elements, using addElement(Object obj) API method of Vector. Set ...

Read More »

List copy example

This is an example of how to make a safe List copy. Making a safe List copy implies that you should: Create a new synchronized ArrayList, using the synchronizedList(List list) API method of Collections. Add elements to the list, using add(Object e) API method of List. Create a new array from the list, using toArray(T[] a) API method of List. ...

Read More »

PriorityQueue example

In this example we shall show you how to use a PriorityQueue. To use a PriorityQueue one should perform the following steps: Create a new Comparator for Integer objects, that is a comparison function, which imposes a total ordering on some collection of objects. Override the compare(Integer i, Integer j) method of Comparator in order to make a specified comparison. Create ...

Read More »