Priority and Daemon threads in Java.

Posted by admin On January - 19 - 2010

Every thread in Java has a priority . By default , the priority is NORM_PRIORITY or 5. There are three such type of Priorities.

NORM_PRIORITY:

MAX_PRIORITY:

MIN_PRIORITY:

Priority in java are just a normal things like we want to set some threads property high and some’s priority very low. it is commom phnomena. you can understand it with the given below code

class Priorty extends Thread
{
 int num;
 public Priorty(int num)
 {
 this.num = num;
 }
 public void run()
 {
 for (int count = 0; count <= num; count++)
 {
 System.out.println("Count = " + count);
 }
 System.out.print(Thread.currentThread().getName());
 System.out.println("Its priority was " + Thread.currentThread().getPriority());
 Thread.currentThread().setPriority(MAX_PRIORITY);
 System.out.println("Its Priority now is " + Thread.currentThread().getPriority());
 }
 public static void main (String args[])
 {
 for (int ctr = 0; ctr < 2; ctr++)
 {
 Priorty Objprior = new Priorty(5);
 Objprior.start();
 Objprior.setName("Thread" + (ctr+1));
 }
 }
}

The result will be like this.

run:
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Thread1Its priority was 5
Its Priority now is 10
Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Thread2Its priority was 5
Its Priority now is 10
BUILD SUCCESSFUL (total time: 3 seconds)

DAEMON THREADS:

Before starting a engine we need to do some task like open the door, check the fuel and some thing like this. so daemon thread is a service provider which is used to start other tasks. like Garbage collction.

Gabage collection is a daemon thread.  Daemon threads are basically is a thread. it is just like a normal thread. but if you want to find the daemon thread you have to fint it via  getDaemon . Daemon is the service provider.

public class TestDaemon implements Runnable
{
Thread Objth1,Objth2;
public TestDaemon()
{
 Objth1 = new Thread (this);
 Objth1.start();
 Objth2 = new Thread(this);
 Objth2.setDaemon(true);
}
public void run()
{
 System.out.println(Thread.activeCount());
 System.out.println(Objth1.isDaemon());
 System.out.println(Objth2.isDaemon());
}
public static void main (String args[])
{
 new TestDaemon();
}
}

The Output will be like this.

run:
2
false
true
BUILD SUCCESSFUL (total time: 1 second)
VN:F [1.6.2_892]
Rating: 0.0/10 (0 votes cast)
VN:F [1.6.2_892]
Rating: 0 (from 0 votes)
Share with others:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Twitter
  • Twitthis
  • Yahoo! Bookmarks
  • Yahoo! Buzz

Leave a Reply

Urdoogle On Facebook

About Me

I will tell you Later

Twitter

    Photos

    Kis galiKabristanDSC09340DSC09302