Results 1 to 4 of 4

Thread: Thread Priorities in java

  1. #1
    Join Date
    Jan 2009
    Posts
    61

    Thread Priorities in java

    Hi, I want to know about the thread priorities. Can anyone help me to get the information about the thread priorities? If you have any program which will help me know more about thread priorities, then give it to me.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Thread Priorities in java

    Hi, when you create any program with different thread, then not all threads are created equal. Some of the threads you want to give with more time than other. Threads that calculate in the background should get low priorities.
    This can be achieved with the help of priorities. As per your requirement you can use java.lang.Thread which defines three mnemonic constants, Thread.MAX_PRIORITY, Thread.MIN_PRIORITY and Thread.NORM_PRIORITY while using thread. Program below will be helpful to you to get more knowledge about it. Look at it.

    Code:
    public class Test 
    {
      public static void main(String[] args) 
    {
        NamedBytePrinter I = new NamedBytePrinter("I");
        NamedBytePrinter WE = new NamedBytePrinter("WE");
        NamedBytePrinter YOU = new NamedBytePrinter("YOU");    
        I.setPriority(Thread.MIN_PRIORITY);
        WE.setPriority(Thread.NORM_PRIORITY);
        YOU.setPriority(Thread.MAX_PRIORITY);
        I.start();
        WE.start();
        YOU.start();  
      }
    }

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Thread Priorities in java

    Hi, I am not having more knowledge about the threads in java. But you can use the following constants to give priorities in java. Just make use of it in your program.
    • Constant - Description
    • 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)

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Thread Priorities in java

    HI, you can use following code to know how the threads priorities work in java.
    JVM provides following constants for priority:

    java.lang.Thread.MIN_PRIORITY = 1
    java.lang.Thread.NORM_PRIORITY = 5
    java.lang.Thread.MAX_PRIORITY = 10

    Code:
    class  thread1 extends Thread
    {
    thread1(String s)
    {
    super(s);
    start();
    }
    public void run()
    {
        for(int i=0;i<3;i++)
    {
          Thread current=Thread.currentThread();
          current.setPriority(Thread.MIN_PRIORITY);
          int p=current.getPriority();
          System.out.println("Thread Name  :"+Thread.currentThread().getName());
          System.out.println("Thread Priority  :"+current);
          }
      }
    }
      class  thread2 extends Thread
    {
    thread2(String s)
    {
    super(s);
    start();
    }
    
    public void run()
    {
        for(int i=0;i<3;i++)
    {
          Thread current=Thread.currentThread();
          current.setPriority(Thread.MAX_PRIORITY);
          int p=current.getPriority();
          System.out.println("Thread Name  :"+Thread.currentThread().getName());
          System.out.println("Thread Priority  :"+current);
          }
      }
    }
    public class ThreadPriority
    {
    public static void main(String args[])
    {  
         thread1 m1=new  thread1("My Thread 1");
         thread2 m2=new  thread2("My Thread 2");
    }
    }

Similar Threads

  1. Synchronize a producer thread and a consumer thread in C#
    By Ayuka in forum Software Development
    Replies: 6
    Last Post: 10-11-2010, 04:04 AM
  2. Replies: 5
    Last Post: 20-10-2010, 12:44 AM
  3. Control thread speed in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 14-02-2010, 05:29 AM
  4. What is the Event Dispatch Thread in Java?
    By Flaco in forum Software Development
    Replies: 5
    Last Post: 14-02-2010, 05:28 AM
  5. Thread scheduling In Java
    By samualres in forum Software Development
    Replies: 5
    Last Post: 12-02-2010, 06:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,913,758.01084 seconds with 17 queries