Posts

Showing posts from February, 2021

Stopping and blocking threads

  Starting a thread: When we create a thread by taking instance of a thread class, the thread will be in its new-born state. To move it to runnable state, we use a method named start( ). When we invoke this method with a thread, java run-time schedules it to run by invoking it run() method. Now the thread will be in its running state. To start a thread we use the following syntax: MyThread t1 = new MyThread(); t1.start(); Stopping a thread: Whenever we want to stop a thread from running further, we may do so by calling its stop() method. This causes a thread to stop immediately and move it to its dead state. It forces the thread to stop abruptly before its completion i.e. it causes premature death. To stop a thread we use the following syntax: t1.stop(); Blocking a Thread: A thread can also be temporarily suspended or blocked from entering into the runnable and subsequently running state by using either of the following thread m

Priority of a Thread (Thread Priority)

  Priority of a Thread /Thread Priority   Each thread have a priority. Priorities are represented by a number between 1 and 10. In most cases, thread scheduler schedules the threads according to their priority (known as preemptive scheduling). But it is not guaranteed because it depends on JVM specification that which scheduling it chooses. 3 constants defined in Thread class: public static int MIN_PRIORITY public static int NORM_PRIORITY public static int MAX_PRIORITY Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10. Example of priority of a Thread:    class  TestMultiPriority1  extends  Thread    {         public   void  run()            {                  System.out.println( "running thread name is:" +Thread.currentThread().getName());                 System.out.println( "running thread priority is:" +Thread.currentThread().getPriority());               }     public   static   void  mai