getPriority() – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 27 Dec 2020 12:00:58 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg getPriority() – Programmerbay https://programmerbay.com 32 32 What are the important Thread Class Methods? https://programmerbay.com/what-are-the-important-thread-class-methods/ https://programmerbay.com/what-are-the-important-thread-class-methods/#respond Thu, 27 Feb 2020 12:31:49 +0000 https://programmerbay.com/?p=6142 As we previously discussed,  Java can create a thread in two ways, either through Thread class or Runnable Interface.

Since, Runnable Interface consists a single method called run() and it is implemented by Thread Class itself. Even though if one creates a thread using Runnable, still requires to make an instance of the Thread class and the object required to pass to it as an argument.

Why we need to create Thread class object even after implementing Runnable Interface?

The answers is, start() method which is responsible for thread execution ( internally calling run method), registering a thread to thread Scheduler and more.

And this start() method resides in Thread class. So, this is the reason, why we need to create a Thread object separately.

Apart from start(), Thread class supports a bunch of useful methods that not only control thread’s behaviour but also in acquiring information of a thread such as thread name,its priority and more

Some of the important and mostly used thread class methods are discussed here

  • wait()
  • sleep()
  • yield()
  • start()
  • isAlive()
  • join()
  • getName()
  • getPriority()
  • notify()

 

sleep()

  • Sleep method puts a thread to sleep
  • It takes milliseconds and nanoseconds as its argument
  • A thread sleeps for specified seconds mentioned in form of parameters
  • When it comes to synchronization, a thread which calls the method never release its lock or monitor
  • It throws interruptedException
  • Syntax:
    • Thread.sleep(long millisec)
    • Thread.sleep(long millisec, int nanosec)

Program:

class ThreadExample extends Thread{
    @Override
    public void run() {
        System.out.println("Child thread is about to sleep");
        try {
            sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("I am child Thread ");
    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();
        System.out.println("I am Main Thread ");
    }
}

Output:

Child thread is about to sleep
I am Main Thread 
I am child Thread

join()

  • Join method puts a thread to wait for calling thread to complete its execution
  • It takes milliseconds and nanoseconds as its argument
  • It provides three types of argument variation, no argument, single and two arguments
  • It ensures a thread termination on which it calls on
  • For example, thread1.join(), it means waits for thread1 to finish
  • Additional feature is, one can mention, threshold time to which extent to wait
  • It throws interruptedException
  • Syntax:
    • this.join()
    • this.join(long millisec)
    • this.join(long millisec, int nanosec)

Program:

class ThreadExample extends Thread{
    @Override
    public void run() {
        System.out.println("Child thread is printing");
       for(int i=0;i<10;i++)
       {
           System.out.println(i);
       }
        try {
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class Main {

    public static void main(String[] args) throws InterruptedException {
ThreadExample thread1 = new ThreadExample();
thread1.start();

        thread1.join();
        System.out.println("I will wait for child thread to join me");
        System.out.println("Now Main thread is executing");
    }
}

Output:

Child thread is printing
0
1
2
3
4
5
6
7
8
9
I will wait for child thread to join me
Now Main thread is executing

 

yield()

  • yield method makes a thread to wait and give a chance to other threads to complete their tasks having same priority
  • It signals thread scheduler that the thread wants to allow other threads to execute themselves
  •  It takes no argument
  • No thread having same priority means it won’t give chance to low priority threads anyway
  • Syntax
    • Thread.yield()

Program:

class ThreadExample extends Thread{
    @Override
    public void run() {


       for(int i=0;i<10;i++)
       {
           yield();
           System.out.println("I am child "+Thread.currentThread().getPriority());
       }

    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();

System.out.println(" Main thread is executing");
        for(int i=0;i<10;i++)
        {

            System.out.println("I am Main "+Thread.currentThread().getPriority());
        }
    }
}

Output:

 Main thread is executing
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5
I am Main 5
I am child 5

 

Wait() & Notify()

wait() and notify() are two companion methods that work together with the aim of providing more control over threads. They provide a way to achieve inter thread communication. These methods reside under Object class.

As name suggest,

  • wait method makes current executing thread to go in suspended state whereas, notify method invokes immediate waiting thread to carry out its task again
  • wait method puts a thread to sleep and release the its lock, so that other can acquire it
  • wait method comes into play where synchronisation exist

Program:

class WaitExample extends Thread
{
 @Override
 public void run() {
 print();
 }
 synchronized void print()
 {
 System.out.println("THis is child ");
 notify();
 }
}
public class Main {
 public static void main(String[] args) {
 WaitExample thread = new WaitExample();
 thread.start();
 synchronized (thread)
 {
 System.out.println("I am in synchronised :");
 try {
 thread.wait();
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 System.out.println("I am in invoked :");
 }

Output:

anotherone

isAlive()

  • isAlive is a boolean method to check whether a thread is running or not
  • It checks on calling thread, if it returns true, then thread is currently running otherwise terminated
  • It is an alternate to join() method

Program:

class ThreadExample extends Thread{
    @Override
    public void run() {


       for(int i=0;i<10;i++)
       {
           System.out.println("I am child ");
       }

    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();
System.out.println("Child thread is running or not :"+thread1.isAlive());
    }
}

Output:

I am child 
I am child 
I am child 
I am child 
I am child 
I am child 
I am child 
I am child 
I am child 
I am child 
Child thread is running or not :true

start(), getName() or getPriority()

start():  start() method which is responsible for thread execution ( internally calling run method), registering a thread to thread Scheduler and more.

getName(): It is used to get the name of a thread

getPriority(): It is used to get the priority of a thread

class ThreadExample extends Thread{
    @Override
    public void run() {

System.out.println("This child");
System.out.println("Name : "+ Thread.currentThread().getName());
        System.out.println("Priority : "+ Thread.currentThread().getPriority());

    }
}
public class Main {

    public static void main(String[] args) {
ThreadExample thread1 = new ThreadExample();
thread1.start();
System.out.println("Child thread is running or not :"+thread1.isAlive());
    }
}

Output:

This child
Child thread is running or not :true
Name : Thread-0
Priority : 5

 

Similarly,

setPriority(): to set a thread priority

setName(): to set a thread name

]]>
https://programmerbay.com/what-are-the-important-thread-class-methods/feed/ 0