James Gosling: idealism, the Internet and Java, Pt I

Saturday, 27 January 2018

Java Multi threaded programming basics

All modern computer hardware architectures and operating systems are capable and optimized for multi processor system and multi threaded system. 

Process Vrs Thread:

Process is heavy weight where as Thread is light weight. Thread is light weight because process uses it's own address space and thread shares the Process's address space. So Threads are considered to be subset of a process. more over threads consumes less OS resources in terms of physical memory and uses CPU time at most.

Junior programmers think concurrency is hard. 
Experienced programmers think concurrency is easy. 
Senior programmers think concurrency is hard. 
                                                                                  _Java Concurrency Essentials by Martin


Java supports implementing multi threaded applications using java core API and keywords.
Java Run time in short JRE closely works with native operating system at run time to run applications in multi threaded environment.

Java Support for Thread:

Keywords: synchronized, volatile

API support: Basically Runnable Interface, Thread class, Object class and java.util.concurrency package [> java 5] and few other API

Java categorizes Threads into 2 types.

1. Daemon Threads

Daemon threads mostly runs with less priority to compute some not so important task quickly. periodically. [EX: finalize() of Object class (Not advised to override at all. instead use finally block)] and JVM itself starts few daemon threads internally.
These Threads are active until all normal threads are terminated. The main thread is the last normal thread that gets terminated when the application is closed. Hence once this main thread is terminated all daemon threads are terminated subsequently and application is closed.

NOTE: Any threads can be marked as daemon thread using Thread class setDaemon().

2. User Threads
User threads are any threads that programmer creates using java api to compute some tasks concurrently which executes concurrently with other threads.

Advantages of Multi thread Programming:

     Let's consider a use case which demonstrates multi threaded environment.
  1. Simple buffer which stores data temporarily while computing some task.
  2. Buffer size is 10 and dynamically can not be changed.
  3. Producer algorithm: Sequence of instructions which produces data for this buffer.
  4. Consumer algorithm: Sequence of instructions which consumes data from this buffer.
  5. Once buffer is full, delete all elements so that producer algorithm starts producing new feed.
  6. Consumer not allowed to consume data unless buffer is full and empties the buffer
  7. Once it consumes all elements then, let's producer know that it can start producing elements.

This above problem can be considered as a producer and consumer problem which can be solved in number of ways. But it is always good idea to use multiple threads.

How Multi threaded environment enhances the application performance?

Multi threaded programs uses the modern computers operating system resources at maximum, hence gives       
      1. High throughput,
      2. Faster execution,
      3. Quick responsiveness are few among others

Let's see how producer and consumer problem in short PC Problem can be solved using Java:

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

 * Buffer implementation with ArrayList and with Producer consumer problem impl
 * using traditional synchronization or implicit lock
 * @author Nagasharath
public class BufferImpl {

    private CopyOnWriteArrayList<String> buffer;

    private final short SIZE = 10;

    private boolean isBufferFull = false;

    public BufferImpl() {
        buffer = new CopyOnWriteArrayList<String>();

* @throws InterruptedException
private void producesFeed() throws InterruptedException {
    synchronized (this) {

        while (true) {
            if (isBufferFull) {
                System.out.println("Started Consuming!....\n");

        if (SIZE <= buffer.size()) {
            isBufferFull = true;
            System.out.println("Buffer full. Production halts...\n");

* @throws InterruptedException
private void consumesFeed() throws InterruptedException {
        synchronized (this) {
            while (true) {
                Iterator<String> iter = buffer.iterator();
                while (iter.hasNext()) {
                    String next = iter.next();
                    System.out.println("Consuming element: " + next);
                isBufferFull = false;
                System.out.println("Started producing!....");

* @author Nagasharath
class ProducerThread extends Thread {
    public void run() {
           try {
            } catch (InterruptedException e) {


* @author Nagasharath
class ConsumerThread extends Thread {
    public void run() {
        try {
        } catch (InterruptedException e) {


    public static void main(String[] args) {
    BufferImpl impl = new BufferImpl();
    ProducerThread producerThread = impl.new ProducerThread();
    ConsumerThread consumerThread = impl.new ConsumerThread();


Let us simplify the above program

Variables used in the program:

        private CopyOnWriteArrayList<String> buffer;

       private final short SIZE = 10;

        private boolean isBufferFull = false;

buffer is of type CopyOnWriteArrayList [COWA] to store elements. CopyOnWriteArrayList is fail safe data structure which is suitable for concurrent programs. 
Do not forget to run the same program by replacing CopyOnArrayList with ArrayList. If this is the case the program throws ConcurrentModificationException as a result as ArrayList Iterator is fail fast behavior.   

SIZE is a constant of type short which holds the size of COWA data structure

isBufferFull is a switch which holds whether the buffer size is full or not. 

Method to implement Producer algorithm:

 1     /**
 2        * @throws InterruptedException
 3        */
 4     private void producesFeed() throws InterruptedException {
 5         synchronized (this) {
 7             while (true) {
 8                 if (isBufferFull) {
 9                     System.out.println("Started Consuming!....\n");
10                     notify();
11                     wait();
12                 }
14             buffer.add(String.valueOf(System.nanoTime()));
15              if (SIZE <= buffer.size()) {
16                 isBufferFull = true;
17                 Thread.sleep(10000);
18                 System.out.println("Buffer full. Production halts...\n");
19             }
20             }
21         }
22     }

producesFeed() uses java keyword synchronized, Object class notify(), Object class wait() and Thread class sleep() to get the job done.

Line 5: keyword synchronized is used to gain the monitor lock on this object. Here this  refers to the    PCProblem object. Lock ensures mutual exclusion. These are of 2 types Implicit locks and reentrent locks. 

synchronized key word is of type implicit lock also known as intrincic lock.

NOTE: Use synchronization judicially because it influences the performance of application. [Slows down the speed of program]   

Let's see more about synchronization and monitor locks in upcoming posts.

Line 7: Initiates the infinite while loop which runs continuously.

Line 10: calls notify() which is one of the inter thread communication Object class api to tell next waiting thread to start it's communication

Line 11: calls wait() which is again one of the inter thread communication Object class api to tell current thread to wait until next running thread notifies.

Line 14: buffer starts holding current time in nano seconds as element of String type. This will be continues till the size reaches 10.
Line 15: checks the above condition

Line 17: Once the SIZE reaches limit to 10 calls Thread.sleep() to make the current running thread to sleep for 10 seconds. [Used here only for demo sake and to simulate the processing behavior]

 1     /**
 2      * @throws InterruptedException
 3      */
 4     private void consumesFeed() throws InterruptedException {
 5         synchronized (this) {
 6             while (true) {
 7                 Iterator<String> iter = buffer.iterator();
 8                 while (iter.hasNext()) {
 9                         String next = iter.next();
10                         System.out.println("Consuming element: " + next);
11                         buffer.remove(buffer.indexOf(next));
12                   }
13                 isBufferFull = false;
14                 System.out.println("Started producing!....");
15                 notify();
16                 wait();
17             }
18         }
19     }

Line 5: Acquires lock on PCProblem Object 

Line 7: Iterator of CopyOnWrireArrayList to iterate over List for processing it. The basic difference between ArrayList and COWA is Fail fast behavior. ArrayList is fail fast and COWA is Fail safe suitable for cuncurrent behavior. 

Note: Never use for (string ele : listObj) { .... } to modify elements in a list. Instead use Iterator.

Line 11: Once element is consumed remove the element from list.

Line 13: Make flag isBufferFull false once all data is consumed.

Line 15 and Line 16: Call to notify() and wait() to tell the producer thread to start it's execution and the latter one to make current thread to wait respectively.


 1     /**
 2      * @author Nagasharath
 3      *
 4      */
 5     class ProducerThread extends Thread {
 6         @Override
 7         public void run() {
 8               try {
 9                 producesFeed();
10             } catch (InterruptedException e) {
12             }
13         }
14       }
16     /**
17      * @author Nagasharath
18      *
19      */
20     class ConsumerThread extends Thread {
21         @Override
22         public void run() {
23             try {
24                 consumesFeed();
25              } catch (InterruptedException e) {
27             }
28         }
29       }

The above snippet creates 2 Threads one is Producer thread and the other is Consumer thread. 

Line 1 to Line 16: ProducerThread extends Thead class [part of java core API] then at run time JVM considers closely works with operating system to run this producesFeed() as a separate thread. 

Line 16 to Line 29: ConsumerThread extends Thread class which runs consumerThread() as a seperate thread. In our use case Producer and Consumer executes simultaneously.

 1        public static void main(String[] args) {
 2             BufferImpl impl = new BufferImpl();
 3             ProducerThread producerThread = impl.new ProducerThread();
 4             ConsumerThread consumerThread = impl.new ConsumerThread();
 6             producerThread.start();
 7             consumerThread.start();
 8         }

Line 1: creates main(). Java run time first executes this method as an initial thread. This thread is responsible for creating all other threads. in our case ProducerThread and ConsumerThread.

Line 6 and Line 7: Call to the start() starts producer and consumer threads.

Note: always prefer start() to run() of Thread class to initiate and start new Thread.


Buffer full. Production halts...

Started Consuming!....

Consuming element: 2747727445120473
Consuming element: 2747727445291566
Consuming element: 2747727445310340
Consuming element: 2747727445349593
Consuming element: 2747727445369646
Consuming element: 2747727445377753
Consuming element: 2747727445384153
Consuming element: 2747727445390126
Consuming element: 2747727445395673
Consuming element: 2747727445400793
Started producing!....
Buffer full. Production halts...

Started Consuming!....

Consuming element: 2747737453672999
Consuming element: 2747737453761319
Consuming element: 2747737453798013
Consuming element: 2747737453825319
Consuming element: 2747737453849639
Consuming element: 2747737453871826
Consuming element: 2747737453893159
Consuming element: 2747737453914919
Consuming element: 2747737453936679
Consuming element: 2747737453957586
Started producing!....

That's all for now. Next post explains solution to the same PC problem using concurrency utility classes introduced in Java 1.5.

Happy coding! :)

No comments:

Post a Comment

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...