Multi-threading part 3
In the previous post, we saw how we can create a thread in java. We saw several cases and also got a glimpse on which way is the preferred way and why. In this post I’ll try to share my learnings on various constructors offered by Thread
class and priorities of a thread.
Constructors of Thread class
Thread t1 = new Thread();
Thread t2 = new Thread(Runnable r);
Thread t3 = new Thread(String name);
Thread t4 = new Thread(Runnable r, String name);
Thread t5 = new Thread(ThreadGroup g, String name);
Thread t6 = new Thread(ThreadGroup g, Runnable r);
Thread t7 = new Thread(ThreadGroup g, Runnable r, String name);
Thread t8 = new Thread(ThreadGroup g, Runnable r, String name, long stackSize);
Getting and Setting name of a Thread
Every thread in java has some name. It may be default name generated by JVM or customized name provided by programmer. We can get and set name of a thread by using the following two methods of Thread
class:
public final String getName();
public final void setName(String name);
e.g.
class MyThread extends Thread{}
class Test{
public static void main(String[] args){
System.out.println(Thread.currentThread().getName()); // main
MyThread t = new MyThread();
System.out.println(t.getName()); // Thread-0
Thread.currentThread().setName("MyMainThread");
System.out.println(Thread.currentThread().getName()); // MyMainThread
}
}
Thread Priorities
Every thread in java has some priority. It may be default generated by JVM or customized priority provided by programmer. The valid range of thread priorities is 1 to 10, where 1 is minimum priority and 10 is maximum priority.
Thread
class defines the following constants to represent some standard priorities:
Thread.MIN_PRIORITY = 1
Thread.NORM_PRIORITY = 5
Thread.MAX_PRIORITY = 10
Thread scheduler will use priorities while allocating processor. The thread which is having highest priority will get the chance of execution at the first place. If two thread have same priority, then we cannot expect exact execution order. It depends on thread-scheduler.
Getting and setting priority of a thread
Thread
class defines the following methods to get and set priority of a thread:
public final int getPriority();
public final void setPriority(int p); // p is between 1 to 10(both inclusive). Otherwise, we will get runtime exception IllegalArgumentException
Default Priority
The default priority only for the main
thread is 5, but for all remaining threads default priority will be inherited from parent to child i.e. whatever priority parent thread has the same priority will be there for the child thread.
Let us take an example to understand them in more detail:
class MyThread extends Thread{}
class Test{
public static void main(String[] args){
System.out.println(Thread.currentThread().getPriority()); // 5
// Thread.currentThread().setPriority(17); --> IllegalArgumentException
Thread.currentThread().setPriority(7);
MyThread t = new MyThread();
System.out.println(t.getPriority()); // 7
}
}
Let us take one more example to understand the concept of priorities candidly.
class MyThread extends Thread{
public void run(){
for(int i = 0; i < 3; i++){
System.out.println("child thread");
}
}
}
class Test{
public static void main(String[] args){
MyThread t = new MyThread();
t.setPriority(10);
t.start();
for(int i = 0; i < 3; i++){
System.out.println("main thread");
}
}
}
If we are commenting t.setPriority(10);
, then both main
and child
thread have same priority as 5. Therefore, we cannot expect execution order and exact output. If we are not commenting the above line, then main
thread has a priority 5 and child thread has a priority 10. Therefore, child thread will get the chance first followed by main
thread.
NOTE: Some platforms will not provide proper support for thread priorities.