Priority In Java Multithreading | How To Name a Thread in Java

Priority In Java Multithreading | How To Name a Thread in Java

To Get a better understanding of this topic read the below two post before continuing this post.

What Is Multi Threading ? Why Do We Need Multi Threading In Java - Explained [THEORETICAL] 

How To Achieve Multi Threading in Java ? Extending Thread Class | Implementing Runnable Interface

Multi Threading Using Lamda Expression | Join and IsAlive Methods In Multi Threading In Java


Is there any default names to the the threads in Java ?

Let's run the below code and see.

Code:

public class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Thread t1=new Thread( () -> {

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

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

Thread t2=new Thread(() -> {

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

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

System.out.println(t1.getName());

System.out.println(t2.getName());

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println("Bye !");

}

}


Output :



We can see there is a getter method called getName() through which we can get the Thread names.
Thread-0 and Thread-1 are the names we are getting.

Can we change these Thread Names ?

yes, we can change the thread names and also can define the thread name at the time of creating Thread object itself as shown in the code below.

code :

public class ExampleCode 
{
public static void main(String[] args) throws Exception
{
Thread t1=new Thread( () -> {
for(int i=0;i<5;i++)
{
System.out.println("Hi !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
},"Hi Thread");
Thread t2=new Thread(() -> {
for(int i=0;i<5;i++)
{
System.out.println("Hello !");
try 
{
Thread.sleep(1000);
} catch (Exception e) {System.out.println(e);}
}
});
t2.setName("Hello Thread");
System.out.println(t1.getName());
System.out.println(t2.getName());
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Bye !");
}
}


Output:


In the above code , we have set the Thread names by two ways.

1) for the Hi Thread we have set the name while we are defining the Thread object itself.

2)for Hello Thread we have set the name using the setName() method.

In the output we can see that we have change the default thread names to Hi Thread and hello Thread.


Priority in Java Multithreading:

The Priority of thread ranges from 1 to 10.

where 1 is the lowest Priority and 10 is the Highest Priority.

By default the priority of the thread is set to be Normal Priority (i.e., 5) .

Priority can be set as number 1 to 10 , or even by predefined constants as shown below.


 

code:

package firstcode;


public class ExampleCode 

{

public static void main(String[] args) throws Exception

{

Thread t1=new Thread( () -> {

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

{

System.out.println("Hi !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

},"Hi Thread");

Thread t2=new Thread(() -> {

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

{

System.out.println("Hello !");

try 

{

Thread.sleep(1000);

} catch (Exception e) {System.out.println(e);}

}

});

t2.setName("Hello Thread");

System.out.println(t1.getName());

System.out.println(t2.getName());

System.out.println(t1.getPriority());

System.out.println(t2.getPriority());

t1.setPriority(1);

t2.setPriority(Thread.MAX_PRIORITY);

System.out.println(t1.getPriority());

System.out.println(t2.getPriority());

t1.start();

t2.start();

t1.join();

t2.join();

System.out.println("Bye !");

}

}

Output :



the two 5 in the output is because we are trying to print the default priority of the thread.

then we are setting the priority by two ways as said before.

Then 1 and 10 in outputs are the priorities for our threads after we have set the priorities.

 

Continue Reading :

Thread Safety in Java | Synchronized Keyword | Atomic Wrapper Class : 

https://iamafutureprogrammer.blogspot.com/2021/11/thread-safety-in-java.html

Popular Posts