Topic outline

    • Java is a programming language with many threads, which means we can use Java to create multi-threaded programmes. There are two or more components in a multi-threaded programme that can run simultaneously and each component can handle a different task at the same time allowing optimum use of the available resources , particularly when there are multiple CPUs on your machine.

      Multitasking is, by definition, when common computing resources such as a CPU are shared by different processes. Multi-threading expands the principle of multitasking into software, where complex operations can be subdivided into separate threads within a single programme. In parallel, each of the threads will run. Not only does the OS break processing time between different applications, but also between each thread within an application.


      Example- 


      package examples;


      class threading extends Thread{

      public void run()

      {

      // Displaying the thread that is running 

      System.out.println("Current Running Thread = " + Thread.currentThread().getId());

      }

      }


      public class multithreading_example {

      public static void main(String[] args)

      {

      for(int i=0; i<10; i++)

      {

      // Number of threads 

      threading th = new threading();

      th.start();

      }

      }


      }

       

      A thread's life cycle 


      In its life cycle, a thread goes through different phases. A thread, for instance, is born, begins, runs, and then dies. 


      Stages of a life cycle


      1. Fresh: 

      A new thread in the new state starts its life cycle. It stays in this state until the thread begins with the programme. It is often referred to as a thread to be born. 


      2. Runnable: 

      The thread becomes runnable after a newly born thread is launched. A thread is considered to be performing its task in this state.


      3. Waiting: 

      A thread often shifts to the waiting state as the thread waits for a job to be completed by another thread. Only when another thread signals the waiting thread to begin executing does a thread transition back to the runnable state. 


      4. Timed Waiting: 

      For a given time period, a running thread will enter the timed waiting state. When the time period ends or when the event it is waiting for happens, a thread in this state switches back to the running state.


      5. Terminated (Dead): 

      When it finishes its mission or otherwise terminates, a running thread enters the terminated state.



      Thread Methods


      1. Public Void start()


      Starts the thread with a different execution path, then invokes the run) (method on the Thread object. 


      2. Public Void Run()


      The run method is invoked on that Runnable object if this Thread object has been instantiated using a different Runnable target. 


      3. Public Final Void SetName(Name of string) 


      Change the name of the object to Thread. A getName ()method also exists to retrieve the name.


      4. public final void setPriority(int priority) 


      Sets the priority of this object for the thread. The values that are possible are between 1 and 10. 


      5. public final void setDaemon(boolean on) 


      A true parameter denotes this thread as a thread with a daemon. 


      6. Final Public Void Join(Long Millisec) 


      On a second thread, the current thread invokes this procedure, causing the current thread to block until the second thread finishes, or until the stated number of milliseconds passes.