0

I am writing a code in java to print the output as follows

[spirit]

[of]

[wipro]

but i am facing a problem in setting the priorities of the threads and seeing each threads priorty and not getting output as expected.

  class shobj
  {
   public synchronized void  sharedMethod(String arg)
    { 
  System.out.print("[");
  System.out.print(arg);
  try 
     { 

      Thread.sleep(1000);
     }
    catch(Exception e)
    {
     System.out.println("INTERRUPTED");
    }
    System.out.println("]");
  }
}
  class thread1 implements Runnable
 {
  String arg;
  shobj obj1;
  Thread t;
  public thread1(shobj obj1,String arg)
     {
      this.obj1=obj1;
      this.arg=arg;
      t=new Thread(this);
      t.start();
//    System.out.println(t.currentThread());
      }
  public void run()
       {
        obj1.sharedMethod(arg);
        }
}

 class synchro

   {

  public static void main(String args[])
 {
  shobj ob = new shobj();
  thread1 x1 = new thread1(ob,"spirit");
  thread1 x2 = new thread1(ob,"of");
  thread1 x3 = new thread1(ob,"wipro");
  x3.t.setPriority(Thread.NORM_PRIORITY+3);
  x2.t.setPriority(Thread.NORM_PRIORITY+2);
  x1.t.setPriority(Thread.NORM_PRIORITY+1);


  try
     {
      x1.t.join(); //System.out.println(x1.t.currentThread());
      x2.t.join();//System.out.println(x2.t.currentThread());
      x3.t.join();//System.out.println(x3.t.currentThread());
      }
    catch(Exception e)
     {
    System.out.println("Interruted Exception");
     }
      }
   }     

I am getting output as follows:

[spirit]

[wipro]

[of]

4
  • 1
    What is the output you are getting? What would make you think that you would get the above output? Just because one thread has a higher priority does not mean that the other threads will not run (especially on a multi-processor machine). Commented May 20, 2013 at 18:25
  • 1
    Given your code I would expect: [sprit[of[wipro]]] or some variation of it especially given that the threads have started BEFORE you set their priorities. Commented May 20, 2013 at 18:26
  • 3
    Setting a thread priority is just a hint, it is up the JVM and the processor to honour those Commented May 20, 2013 at 18:27
  • 3
    The output you are getting is correct. It is your understanding of thread that is incorrect I am afraid. Commented May 20, 2013 at 18:35

2 Answers 2

1

See How are Java Thread priorities translated to an OS thread priority? how the thread priority is mapped to the native OS. There is no guarantee that different thread priorities in java lead to different priority on OS level.

Sign up to request clarification or add additional context in comments.

Comments

0

Priority is just a hint to the OS. If you have plenty of free CPU, all thread which want to run, can run.

This means all the threads in your case could run in any order which is what having multiple threads is designed to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.