Multithreading in Java
Skip to main content

Multithreading in Java

 

multithreading in java, multi threading in java, java multithreading tutorial, multi threading concept in java, reating multiple threads in java, multi threading java,multi threaded programming in java, java multithreading course, learn java multithreading, multithreading in java example, multithreading in javascript, multithreading in java program, advantages of multithreading in java


Multithreading in Java

Multithreading in Java:- 

Two and more thread execute on same time is called a multi threading. When two thread working at a time then thread perform independently but its path is different. so at execution time if any exception caught in any  one thread then that does not effect on other thread. Multi Threading is used for achieving a multi-taking.


Thread :- 

Thread is a lightweight process. Thread is a basic unit of CPU and which is perform its task independently because its path is different. Thread is a pre defined class in Java which is belongs to the Java.lang package.


Most Read:- Difference between Method overloading and overriding


Advantage of Multi-Threading :-

1.      Thread is independent so it does not block the user.

2.      Multi threading perform more than one task using at a time so it is saving the user time.

3.      Multi threading is increase the performance CPU.

4.      Decrease cost of maintenance.

5.      Improvised GUI responsiveness.


Disadvantage of Multi-Threading :-

1.      Complex Debugging

2.      complex testing process

3.      Increase potential for deadlock occurrence

4.      Unpredictable result

5.      Increase difficulty for writing a program.

 

Life cycle of Multi Threading:-


1.      New State: When a new thread is created that is a come at New State. There is code is ready to run.

2.      Runnable State: In this state thread is ready to move to running state at any time. Multi threading  process is given a amount of time for a particular thread. When one thread is timeout after other thread at new state thread is ready to runnable state. It is not blocked and dead.

3.      Running State: In this state thread is running state by using of run() method there thread execution of it's action with given time slice by CPU.

4.      Waiting State: In this state thread is waiting some time then that is waiting state. When a thread call and it is going to sleep mode or wait mode by call sleep() or wait() method then thread move to moving state.

5.      Terminate: Thread is goes to terminate with some reason: First, Thread is normally terminated it means code is successfully to completion. Second, if any error is occurred then it is terminate.

      

       Most Read:-Exception Handling in Java

     

      Commanly methods used by threading:

  1. run(): run methid is used to perform a action for a thread.
  2. start(): starts method is used in thread for execution of thead then JVM call the run method to start the Thread.
  3. sleep(long miliseconds): sleep method is used in currently executing thread then thread goes sleep mode for some milliseconds.
  4. join(): When used join method in executing thread then a threads is waits for die.
  5. join(long miliseconds):When used join method in executing thread then a threads is waits for die for the specified milliseconds.
  6. suspend(): suspend method is used to suspend the thread.
  7. resume(): resume method is used to resume the suspended thread.
  8. stop(): stop method is used to stop the thread.
  9. getName(): getName method is used to returns the name of the thread.
  10. setName(String name): setName is used to change the name of the thread.
  11. currentThread(): currentThread method is used to return the reference of currently executing thread.
  12. getId(): getId method is used to return the id of the thread.
  13. getState(): getState method is used to return the state of the thread.
  14. isAlive(): isAlive method is used to test the thread, if the thread is alive. isAlive method return the boolean type value.
  15. interrupt(): interrupt method is used to Interrupt the thread.




Example of Threading:-


1. class is extends by Thread class :


  class ThreadExample extends Thread{
         public void run(){
           System.out.println("Thread is running in ThreadExample class");
      }
     public static void main(String [] args){
          ThreadExample exam = new ThreadExample();
           exam.start();
   }
  }
Output:- Thread is running in ThreadExample class

2. Class is implements by Runnable interface :

  class ThreadExample implements Runnable{
         public void run(){
           System.out.println("Thread is running in ThreadExample class");
      }
     public static void main(String [] args){
          ThreadExample exam = new ThreadExample();
          Thread thread = new Thread(exam);
           thread.start();
         System.out.println("Thread is  goes to sleep mode");
          thread.sleep(1000);
          System.out.println("Thread is again start");
        thread.start();
   }
  }
Output:- Thread is running in ThreadExample class
Thread is  goes to sleep mode
Thread is again start
Thread is running in ThreadExample class





 

Post a Comment

0 Comments