Social Icons

twitterfacebookgoogle pluslinkedinemail

Thursday 29 August 2013

what is thread in java...?

Thread is a light wait process.In java, Thread is class which are extends to the our program.

Thread class have a run method which will be executed the thread.start( ) method will be call the run method and execute whenever the program run Thread are executed in parallel so the which thread are first execute and which are second is not decided.

class Thread1 extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread 1: " + i);
}
}
}
class Thread2 extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread 2: " + i);
}
}
}
class Thread3 extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
System.out.println("Thread 3: " + i);
}
}
}

class demo
{
public static void main(String args[])
{
Thread1 t1 = new Thread1();
Thread2 t2 = new Thread2();
Thread3 t3 = new Thread3();

t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.NORM_PRIORITY);
t3.setPriority(Thread.MAX_PRIORITY);

t1.start();
t2.start();
t3.start();
}
}

Thread class has a setPriority() method which will be setting the priority and consider which thread are execute in first.

value of the setPriority() method:

MIN_PRIORITY     :1
NORM_PRIORITY :5
MAX_PRIORITY    :10

Default priority is NORM_PRIORITY.

No comments :

Post a Comment

 

Free Advertisement

Free Advertisement

Free Advertisement

Free Advertisement