Java Multithreading

Threads.  Thread is also known as lightweight process. Java multithreading allows to process multiple task simultaneously with less overhead than process. Mostly use to monitor session, background synchronization etc.

Content

Processes : These are often application or programs running in their own memory space and runtime resources. In simple term when you run new program with main() method . It is basically a new process and have their own private memory, and resources.

Thread: The path of execution of a process is known as Thread.  A process can have more than one thread. In a simple program with main() method.This program contains a user defined thread with main() method. While with user threads there are system threads like memory management and signal handling.

  • Threads are also known as lightweight processes.
  • A process must contain at least one Thread.
  • All threads inside a process can communicate  each other via shared memory space (Shared variable)

Creating first Java Thread There are two way to create java threads.

    • In both cases we need to override run() method and this method is part of a thread. A thread start from start from starting of run() and ends when run() ends.
    • To start thread, Thread object needs to be start using threadObj.start() which invoke the thread and execute the run method.
  1. Implementing Runnable interface :

2.  Extending Thread class 

 Implementing Runnable vs Extending Thread :

  • Implementing Runnable allows you to extends another class while, if you extend Thread class you will not able to extends any other class.
  • Extending thread, each thread needs to associate with unique object.
  • Implementing Runnable allows you to associate single object to as many threads you want.
  • Sharing same object to multiple thread also avoids unnecessary object creation and saves memory.

Thread State and Life Cycle

state-machine-example-java-6-thread-states Source: uml-diagram.org

A thread can be one of the following state (java 1.5+)

  • NEW : A thread has not started yet.
  • RUNNABLE : Thread is running state but it can be in state of waiting.
  • BLOCKED :  Thread is waiting to acquire monitor lock to enter into a synchronized block/method after calling Object.wait()
  • WAITING : A thread is in waiting state due to calling one of the following methods
    • Object.wait() : It causes current thread to wait until it been notified by method notify() or notifyAll().
    • Object.join() : Waits for current thread to die.
    • LockSupport.park : Disables the current thread for thread scheduling purposes unless the permit is available.
  • TIMED_WAITING : Current thread is waits for another thread for specified time to perform the aciton.
    • Thread.sleep (long timeInMilliSecond) : Makes current thread to cease the execution for specified time.
    • Object.wait (long timeInMilliSecond) :  Causes current thread to wait for specified time until time elapsed or get notified by notify() or notifyAll().
    • Thread.join (long millis) : Current thread waits for specified time to die the thread.
    • LockSupport.parkNanos (long nanoSeconds) : Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available.
    • LockSupport.parkUntil ()
  • TERMINATED : When thread completed its execution.

<Post in progress to explain thread states>

Daemon Thread Daemon threads are threads to which JVM don’t watch to completed or not. So how JVM works, As soon as all user threads finished processing , JVM terminated itself. JVM doesn’t wait to finish daemon threads. Garbage collector and other housekeeping threads are inbuild daemon threads.

  • User created threads are non-daemon by default, since it’s created by main() thread which is by default a non-daemon and newly created thread inherit daemon property from the thread it was created.
  • To set a user created thread as daemon, you need to set by Thread.setDaemon(true) (Make sure you set a daemon before calling start()).

Example: Daemon Thread

Notice, thread marked as daemon thread didn’t completed its execution, because JVM only waited for non-daemon thread and as soon as it completed, JVM also terminated.

Leave a Reply

Your email address will not be published. Required fields are marked *