Results 1 to 7 of 7

Thread: Write A program Code In Java To create threads of different priority

  1. #1

    Write A program Code In Java To create threads of different priority

    i went through one blog of this site on thread priority. thanks for such information. but now i need help from you people also for my code. please help asap. Member!!

  2. #2
    Super Moderator angad's Avatar
    Join Date
    Aug 2010
    Blog Entries
    Rep Power

    Thumbs up Re: Write A program Code In Java To create threads of different priority

    well you could refer this code. if this satisfies your query then let me know
    /*   Create four threads with priority 1,3,5,7 
    respectively. Update a counter in each of the 
    threads for ten sec. print the final value 
    of the count from each thread. */ 
    class ThreadPrio implements Runnable
    	public long count=0;
        Thread t;
        private volatile boolean running=true;
        public ThreadPrio(int p)
        	t=new Thread(this);
        public void run()
        public void start()
        public void stop()
    public static void main(String[] args) throws InterruptedException
    	ThreadPrio t7=new ThreadPrio(7);
    	ThreadPrio t5=new ThreadPrio(5);
    	ThreadPrio t3=new ThreadPrio(3);
    	ThreadPrio t1=new ThreadPrio(1);
        System.out.println("Main going to sleep for 10 secs. Plz wait patiently.");
        System.out.println("Thread with Priority 1 :"+t1.count);
        System.out.println("Thread with Priority 3 :"+t3.count);
        System.out.println("Thread with Priority 5 :"+t5.count);
        System.out.println("Thread with Priority 7 :"+t7.count);
    } Member!!

  3. #3

    Re: Write A program Code In Java To create threads of different priority

    good work angad sir and ya thanks to sachdev also for putting this question before me beacuse i was going to ask this. Member!!

  4. #4

    Re: Write A program Code In Java To create threads of different priority

    thaks tf4u for providing such solutions and helping us. Member!!

  5. #5

    Re: Write A program Code In Java To create threads of different priority

    good work angad. liked the program. Member!!

  6. #6

    Re: Write A program Code In Java To create threads of different priority

    he following example demonstrates two threads at different priorities, which do not run on a preemptive platform in the same way as they run on a nonpreemptive platform. One thread is set two levels above the normal priority, as defined by Thread.NORM_PRIORITY, and the other is set to two levels below it. The threads are started and allowed to run for ten seconds. Each thread executes a loop, counting the number of iterations. After ten seconds, the main thread stops both threads. The number of times that each thread made it through the loop is then displayed.
    // Demonstrate thread priorities.
    class clicker implements Runnable {
    int click = 0;
    Thread t;
    private volatile boolean running = true;
    public clicker(int p) {
    t = new Thread(this);
    public void run() {
    while (running) {
    public void stop() {
    running = false;
    public void start() {
    class HiLoPri {
    public static void main(String args[]) {
    clicker hi = new clicker(Thread.NORM_PRIORITY + 2);
    clicker lo = new clicker(Thread.NORM_PRIORITY - 2);
    try {
    } catch (InterruptedException e) {
    System.out.println("Main thread interrupted.");
    // Wait for child threads to terminate.
    try {
    } catch (InterruptedException e) {
    System.out.println("InterruptedException caught");
    System.out.println("Low-priority thread: " +;
    System.out.println("High-priority thread: " +;
    The output of this program, shown as follows when run under Windows 98, indicates that the threads did context switch, even though neither voluntarily yielded the CPU nor blocked for I/O. The higher-priority thread got approximately 90 percent of the CPU time.

    Low-priority thread: 4408112
    High-priority thread: 589626904

    Of course, the exact output produced by this program depends on the speed of your CPU and the number of other tasks running in the system. When this same program is run under a nonpreemptive system, different results will be obtained.

    One other note about the preceding program. Notice that running is preceded by the keyword volatile.Volatile is used here to ensure that the value of running is examined each time the following loop iterates:

    while (running) {

    Without the use of volatile, Java is free to optimize the loop in such a way that the value of running is held in a register of the CPU and not necessarily reexamined with each iteration. The use of volatile prevents this optimization, telling Java that running may change in ways not directly apparent in the immediate code.
    Last edited by Harsh; 08-08-2011 at 01:10 PM.

  7. #7

    Re: Write A program Code In Java To create threads of different priority

    Thread.MIN_PRIORITY -The maximum priority of any thread (an int value of 10)
    Thread.MAX_PRIORITY -The minimum priority of any thread (an int value of 1)
    Thread.NORM_PRIORITY - The normal priority of any thread (an int value of 5)
    setPriority() - This is method is used to set the priority of thread.
    getPriority() - This method is used to get the priority of thread.

Similar Threads

  1. Replies: 0
    Last Post: 11-03-2011, 06:20 PM
  2. Replies: 7
    Last Post: 08-06-2011, 09:41 AM
  3. Replies: 1
    Last Post: 02-27-2011, 08:34 AM
  4. Replies: 3
    Last Post: 02-21-2011, 08:50 AM
  5. Replies: 8
    Last Post: 02-20-2011, 06:13 AM

Members who have read this thread : 3

You do not have permission to view the list of names.

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Disclaimer: Users of are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.