Saturday, December 3, 2016

multi threaded - Multithreading & Synchronization

multi threaded - Multithreading & Methods

        Let’s understand the thread first Multi threading 

 What does mean by thread? Basically threads are part of single program or in other words threads are a sub program or sub process, which are independents if any exception is occur in one thread then it does not affect the other threads because threads are independent .Multithreading & Methods

In operating system there are multiple processes and in one processes there are multiple threads. Threads shares common memory area. When multiple threads run simultaneously is known as multi threading.

Multi threading is used to achieve multitasking. Now understand the term multitasking, when multiple task executes simultaneously then it is known as multitasking. Multitasking is used for CPU utilization.
Multithreading & Methods

Thread Life Cycle
In thread life cycle, it has five states which are the following

o   New: this state begins the thread and the thread will be in same state until the program start the thread.

o   Runnable: once the thread is started then thread state become runnable. In this state thread is ready for execution.

o   Running: when thread start its execution then it state become Running which means thread is running

o   Block: when the another thread will come in running state then the current thread will go in block state, this block state is also called waiting state.

o   Terminate: when thread execution complete or its run method exit then it state become terminate, which indicate the end of the thread.

All these thread states in java are controlled by JVM. 

Creating thread in Java
In java there are two ways to create the thread in Java.
  o   By extending the Thread class
  o   By implement the Runnable interface

Multithreading & Methods

Thread Common Methods


  • public void run(): is used to perform action for a thread.
  • public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
  • public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
  • public void join(): waits for a thread to die.
  • public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
  • public intgetPriority(): returns the priority of the thread.
  • public intsetPriority(int priority): changes the priority of the thread.
  • public String getName(): returns the name of the thread.
  • public void setName(String name): changes the name of the thread.
  • public Thread currentThread(): returns the reference of currently executing thread.
  • public intgetId(): returns the id of the thread.
  • public Thread.StategetState(): returns the state of the thread.
  • public booleanisAlive(): tests if the thread is alive.
  • public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
  • public void suspend(): is used to suspend the thread(depricated).
  • public void resume(): is used to resume the suspended thread(depricated).
  • public void stop(): is used to stop the thread(depricated).
  • public booleanisDaemon(): tests if the thread is a daemon thread.
  • public void setDaemon(boolean b): marks the thread as daemon or user thread.
  • public void interrupt(): interrupts the thread.
  • public booleanisInterrupted(): tests if the thread has been interrupted.
  • public static booleaninterrupted(): tests if the current thread has been interrupted.
Example with Runnable Thread

       public class InterfaceDemo implements Runnable {
                          @override
       public void run() { //thread class method
          system.out.println("this is Runnable Thread");
       }
       public static void main(String [] arg){
       InterfaceDemo demo = new InterfaceDemo();
      Thread thread = new Thread(demo);
      thread.start();
      }
      }
Result: This is Runnable Thread.

Multithreading & Methods

        Synchronization

Synchronization is more powerful mechanism in Java. It is used with threads to control the multiple access of threads. When you want that multiple thread will access one by one at the shared resource then synchronization is a better option there are two types of synchronization in thread
  o   Mutual Exclusive: when you want to keep interfering threads from each other while sharing the data then mutual exclusive is the best option. Mutual exclusive is further divided into three types
·         Synchronized method
·         Synchronized Block
·         Static synchronized

  o   Inter Thread Communication: when you want that the threadsare communicate with each other than inter thread communication is the best option. So, for inter thread communication we have to implements following method of Object Class

·         wait()
·         notify()

·         notifyAll()

No comments:

Post a Comment