Tuesday, 20 January 2015

What are the Methods of Thread Class

The join() method:

The join() method waits for a thread to die. In other words, it causes the currently running threads to stop
executing until the thread it joins with completes its task.

Syntax:

public void join() throws InterruptedException
public void join(long miliseconds) throws InterruptedException

For Example:

class Test extends Thread{
public void run(){
for(int a=1;a<=6;a++){
try{
Thread.sleep(500);
}catch(Exception ex){
System.out.println(ex);
}
System.out.println(a);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
t1.start();
try{
t1.join();
}catch(Exception ex){
System.out.println(ex);
}
t2.start();
t3.start();
}
}

Output:
1
2
3
4
5
1
1
2
2
3
3
4
4
5
5
6
6

As you can see in the above example,when t1 completes its task then t2 and t3 starts executing.

Example of join(long miliseconds) method:

class Test extends Thread{
public void run(){
for(int a=1;a<=5;a++){
try{
Thread.sleep(500);
}catch(Exception ex){System.out.println(ex);}
System.out.println(i);
}
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
Test t3=new Test();
t1.start();
try{
t1.join(1500);
}
catch(Exception ex){
System.out.println(ex);}
t2.start();
t3.start();
}
}

Output:
1
2
3
1
4
1
2
5
2
3
3
4
4
5
5


In the above example,when t1 is completes its task for 1500 miliseconds(3 times) then t2 and t3 starts executing.

getName(), setName(String) and getId() method of Thread class:

public String getName()
public void setName(String name)
public long getId()

class Test extends Thread{
public void run(){
System.out.println("this is run method");
}
public static void main(String []args){
Test t1=new Test();
Test t2=new Test();
System.out.println("Name of t1:"+t1.getName());
System.out.println("Name of t2:"+t2.getName());
System.out.println("id of t1:"+t1.getId());
t1.start();
t2.start();
t1.setName("Vishwa Singh");
System.out.println("After changing name of t1:"+t1.getName());
}
}

Output: Name of t1:Thread-0
Name of t2:Thread-1
id of t1:8
this is run method
After changling name of t1:Vishwa Singh
this is run method


The currentThread() method:

The currentThread() method returns a reference to the currently executing thread object.
Syntax:

public static Thread currentThread()

Example of currentThread() method:

class Test extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String args[]){
Test t1=new Test();
Test t2=new Test();
t1.start();
t2.start();
}
}

Output: Thread-0
Thread-1


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
schedular schedules the threads according to their priority (known as preemptive scheduling). But it is not
guaranteed because it depends on JVM specifification that which sheduling it chooses.

3 constants defined in Thread class:

1. public static int MIN_PRIORITY
2. public static int NORM_PRIORITY
3. 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 Test 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 main(String args[]){
Test t1=new Test();
Test t2=new Test();
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
}
}

Output: running thread name is:Thread-0
running thread priority is:10
running thread name is:Thread-1
running thread priority is:1


No comments:

Post a Comment