title: Setting Priority for Thread Java
body: <p>this may be a very silly question but I tried setting priority for a few threads in a java program but it seems like the order in which I start each thread only determines which one is more favored.</p>
  
  <p>I am trying to run 4 Tasks and increment 4 separate counters for each Task. Then I am putting the main thread to sleep for 10 sec while the 4 Threads run and increment their counters.Once the Main thread is revived the program terminates after printing the final count values.</p>
  
  <p>What I got as output was something like this:</p>
  
  <p><strong>Job 1:</strong> 5876   </p>
  
  <p><strong>Job 2:</strong> 6546   </p>
  
  <p><strong>Job 3:</strong> 6020   </p>
  
  <p><strong>Job 4:</strong> 0</p>
  
  <p>Even though the thread running job 4 got highest priority.</p>
  
  <p>I think I have some serious conceptual problem here...And maybe what I am trying to do is not what I am doing in the program. Please help me. Thank you in Advance.</p>
  
  <pre><code>class Task1 implements Runnable
  {
      public int count=0; 
      public void run()
      {
          while(true)         
          count++;
  
      }
  }
  
  class Threadcount
  {
      public static void main(String[] args)
      {
  
          Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
          Task1 job1=new Task1();
          Task1 job2=new Task1();
          Task1 job3=new Task1();
          Task1 job4=new Task1();
          Thread t1=new Thread(job1);
          Thread t2=new Thread(job2);
          Thread t3=new Thread(job3);
          Thread t4=new Thread(job4);
  
          t1.setPriority(1);
          t2.setPriority(3);
          t3.setPriority(5);  
          t4.setPriority(7);  
  
          try
          {   
              Thread.sleep(10000);
          }
          catch(Exception e)
          {
          }
  
          t1.start();
          t2.start();
          t3.start();
          t4.start();
  
  
  
          System.out.println("Job 1:  "+job1.count);
          System.out.println("Job 2:  "+job2.count);
          System.out.println("Job 3:  "+job3.count);
          System.out.println("Job 4:  "+job4.count);
          System.exit(1);
  
      }
  
  }
  </code></pre>