by Dinesh Thakur

 

 class Counter implements Runnable

  {
     Thread Thrd;
     int Countr = 0;
     volatile boolean goflag;
     public Counter(int p1)
       {
         Thrd = new Thread(this);
         Thrd.setPriority(p1);
       }
         public void start()
          {
            goflag = true;
            Thrd.start();
          }
            public void run()
            {
              while (goflag) Countr++;
            }
              public void end()
               {
                 goflag = false;
               }
  }
       class JavaExampleThreadPriority
        {
           public static void main(String aa[])
            {
                Counter ThrdOne = new Counter(Thread.NORM_PRIORITY + 2);
                Counter ThrdTwo = new Counter(Thread.NORM_PRIORITY + 1);
                Counter ThrdThree = new Counter(Thread.NORM_PRIORITY - 1);
                Counter ThrdFour = new Counter(Thread.NORM_PRIORITY - 2);
                ThrdOne.start();
                ThrdTwo.start();
                ThrdThree.start();
                ThrdFour.start();
                try
              {
                     Thread.sleep(1000);
                  }
                 catch (InterruptedException e1) {}
                     ThrdOne.end();
                     ThrdTwo.end();
                     ThrdThree.end();
                     ThrdFour.end();
                     System.out.println("First Thread counted: " + ThrdOne.Countr);
                     System.out.println("Second Thread  counted: " + ThrdTwo.Countr);
                     System.out.println("Third Thread  counted: " + ThrdThree.Countr);
                    System.out.println("Fourth Thread  counted: " + ThrdFour.Countr);
              }
        }
Thread Priority in Java Example