Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Wednesday, May 27, 2015

Multithreading


Threads


Why threads?


Need to handle concurrent processes.

Definition


Single sequential flow of control within a program
For simplicity, think of threads as processes executed by a program
Example:
  • Operating System
  • HotJava web browser

Thread States


A thread can in one of several possible states:

1.Running
  • Currently running
  • In control of CPU

2.Ready to run
  •  Can run but not yet given the chance

3.Resumed
  • Ready to run after being suspended or block

4.Suspended
  • Voluntarily allowed other threads to run

5.Blocked
  • Waiting for some resource or event to occur


Two Ways of Creating and Starting a Thread


1.Extending the Thread class
2.Implementing the Runnable interface

Extending Thread Class


The subclass extends Thread class
  • The subclass overrides the run() method of Thread class

An object instance of the subclass can then be created
Calling the start() method of the object instance starts the execution of the thread
  • Java runtime starts the execution of the thread by calling run() method of object instance.

Two Schemes of starting a thread from a subclass


1.The start() method is not in the constructor of the subclass
  • The start() method needs to be explicitly invoked after object instance of the subclass is created in order to start the thread

2.The start() method is in the constructor of the subclass
  • Creating an object instance of the subclass will start the thread.

Implementing Runnable Interface

The Runnable interface should be implemented by any class whose instances are intended to be executed as a thread
The class must define run() method of no arguments
  • The run() method is like main() for the new thread

Provides the means for a class to be active while not subclassing Thread
  • A class that implements Runnable can run without subclassing Thread by instantiating a Thread instance and passing itself in as the target.


Two Ways of Starting a Thread For a class that implements Runnable


1.Caller thread creates Thread object and starts it explicitly after an object instance of the class that implements Runnable interface is created
  • The start() method of the Thread object needs to be explicitly invoked after object instance is created

2.The Thread object is created and started within the constructor method of the class that implements Runnable interface
  • The caller thread just needs to create object instances of the Runnable class.

Extending Thread Class vs. Implementing Runnable Interface

Choosing between these two is a matter of taste
Implementing the Runnable interface
  • May take more work since we still
  •  ● Declare a Thread object
  •  ● Call the Thread methods on this object

  • Your class can still extend other class

Extending the Thread class
  • Easier to implement
  • Your class can no longer extend any other class


Synchronization : Locking an Object

A thread is synchronized by becoming an owner of the object's monitor
  • Consider it as locking an object

A thread becomes the owner of the object's monitor in one of three ways
  • Option 1: Use synchronized method
  • Option 2: Use synchronized statement on a common object


Deadlocks

where they thread cannot proceed and thus wait indefinitely for others to terminate.

livelock


Consider two threads t1 and t2. Assume that thread t1 makes a change and thread t2 undoes that change. When both the threads t1 and t2 work, it will appear as though lots of work is getting done, but no progress is made. This situation is called a livelock in threads.


Lock Starvation


in a situation where low-priority threads “starve”  or suffer for a long time trying to obtain the lock is known as lock starvation.


No comments:

Post a Comment