There are several methods that are available for efficient utilization of thread to set general properties e.g priority, name. There are methods that give information regarding the state and also allow operations on the thread.
Let us look at a few of them.
void start()
run
method of calling thread.
class MyThread extends Thread{
MyThread(){
super("My First Thread");
}
public void run(){
System.out.println("I am inside run method of MyThread triggered by start");
}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread();
System.out.println("The thread is "+ob);
ob.start();
}
}
The thread is Thread[My First Thread,5,main]
I am inside run method of MyThread triggered by start
static int activeCount()
class MyThread extends Thread{
MyThread(){
super("My First Thread");
}
public void run(){}
}
public class ClassMain{
public static void main(String args[]){
System.out.println("Active count of thread is "+Thread. activeCount());
System.out.println("A new thread added now");
MyThread ob = new MyThread();
ob.start();
System.out.println("Active count of thread is "+Thread. activeCount());
}
}
Active count of thread is 1
A new thread added now
Active count of thread is 2
static Thread currentThread ()
class MyThread extends Thread{
MyThread(){
super("My First Thread");
}
public void run(){
System.out.println("The currently active thread is "+Thread.currentThread());
}
}
public class ClassMain{
public static void main(String args[]){
System.out.println("The currently active thread is "+Thread.currentThread());
MyThread ob = new MyThread();
ob.start();
}
}
The currently active thread is Thread[main,5,main] The currently active thread is Thread[My First Thread,5,main]
long getId()
class MyThread extends Thread{
MyThread(){
super("My First Thread");
}
public void run(){
System.out.println("<Inside run>The id of currently active thread is "+this.getId());
}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread();
ob.start();
System.out.println("<Outside run inside main>The id of currently active thread is "+ob.getId());
}
}
<Outside run inside main>The id of currently active thread is 21 <Inside run>The id of currently active thread is 21
String getName()
class MyThread extends Thread{
MyThread(){
super("My First Thread");
}
public void run(){
System.out.println("<Inside run>The name of currently active thread is "+this.getName());
}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread();
ob.start();
System.out.println("<Outside run inside main>The name of currently active thread is "+ob.getName());
}
}
<Outside run inside main>The name of currently active thread is My First Thread <Inside run>The name of currently active thread is My First Thread
void setName()
public class ClassMain extends Thread{
ClassMain(String s){
super(s);
}
public void run() {
}
public static void main(String args[]){
ClassMain ob = new ClassMain("My_First_Thread");
ob.start();
System.out.println("<Before>The name of ob is "+ob.getName());
ob.setName("My_New_Name");
System.out.println("<After>The priority of ob is "+ob.getName());
}
}
<Before>The name of ob is My_First_Thread <After>The priority of ob is My_New_Name
int getPriority()
class MyThread extends Thread{
MyThread(String name){
super(name);
}
public void run(){
}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread("My first thread");
MyThread ob2 = new MyThread("My second thread");
ob.start();
ob2.start();
System.out.println("The priority of ob is "+ob.getPriority());
System.out.println("The priority of ob2 is "+ob.getPriority());
}
}
The priority of ob is 5 The priority of ob2 is 5
void setPriority()
public class ClassMain extends Thread{
public void run() {
}
public static void main(String args[]){
ClassMain ob = new ClassMain();
ob.start();
System.out.println("<Before>The priority of ob is "+ob.getPriority());
ob.setPriority(10);
System.out.println("<After>The priority of ob is "+ob.getPriority());
}
}
<Before>The priority of ob is 5 <After>The priority of ob is 10
static void sleep(long millisec)
class MyThread extends Thread{
MyThread(String s){
super(s);
}
public void run(){
try{
for(int i=0;i<5;i++){
System.out.println("MyThread:"+i);
Thread.sleep(500); //MyThread sleeps for 500 miliseconds
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread("First_Thread");
ob.start();
try{
for(int i=0;i<5;i++){
System.out.println("Main Thread:"+i);
Thread.sleep(1000); //MainThread sleeps for 1000 miliseconds
}
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
Main Thread:0 MyThread:0 MyThread:1 Main Thread:1 MyThread:2 MyThread:3 Main Thread:2 MyThread:4 Main Thread:3 Main Thread:4
void getState()
class MyThread extends Thread{
MyThread(String s){
super(s);
}
public void run(){}
}
public class ClassMain{
public static void main(String args[]){
MyThread ob = new MyThread("First_Thread");
try{
System.out.println("State of MyThread:"+ob.getState()); //Outputs NEW
ob.start();
System.out.println("State of MyThread:"+ob.getState()); //Outputs RUNNABLE
ob.sleep(2500);
System.out.println("State of MyThread:"+ob.getState()); //Outputs TERMINATED
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
State of MyThread:NEW State of MyThread:RUNNABLE State of MyThread:TERMINATED
This was an overview of methods related to Thread. We will discuss Java I/O in the next lecture.