• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we use setPriority inside a synchronized block

 
Axes Axes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class ThreadTest extends Thread
{

public void run()
{
synchronized
{
System.out.println(Thread.currentThread().getName());
Thread.setPriority(1);
System.out.println(Thread.currentThread().getName());
}
}

public static void main(String app[])
{
ThreadTest t1=new ThreadTest();
ThreadTest t2new ThreadTest();

t1.setName("A");
t2.setName("B");

t1.setPriority(4);
t2.setPriority(2);

t1.start();
t2.start();

}
}

Here when the first thread started to execute, and we are changing
the priority of the current thread inside the syncronized block. so the first thread stop executing inside the syncronized block itself and the next thread is been scheduled. After the completion of my second thread .. the first thread starts the execution from where it stops..
So the concept that when any thread is been executed inside the syncronization block it persues the lock untill that thread completes
after the completion only it will allows the second thread inside the syncronized block becomes FALSE in this scenario. please justify with your comments in this code.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you can't Thread.setPriority(1), as setPriority() is a non-static method. I guess that's just a typo.

Second, setPriority() may well have no effect on a running thread. A more typical use is to create the new thread object, then set its priority before starting the thread.

Third, your synchronisation doesn't look as if it will achieve much. You are synchronising on the instance in run(), but nowhere else. You are failing to synchronise when modifying the objects in main().

Finally, don't rely too much on tight control thread priorities and thread scheduling. Java makes only a few specific guarantees in the API and language specification. If you find out how it behaves, beyond those guarantees, on one operating system and make your program rely on that, then your program probably will not work on another operating system; it may even not work on a different flavour of the same operating system or a different version of Java.
 
Henry Wong
author
Sheriff
Posts: 22514
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can call setPriority() at any time, even if the call drops the priority to a level where it gives up the timeslice, taking a synchronization lock with it... But as you may noticed, it isn't that simple. There are some issues.

Just because you are of higher priority does not mean you can actually execute. If you are waiting for a synchronization lock, you are blocked -- regardless of priority.

You are subjected to the underlying implementation. Some JVM don't preempt a thread instantly, you may wait to timeslice. (not common anymore) Some JVMs will allow the lower priority to run over a higher priority due to other reasons. (mainly windows)

And lastly, most JVMs support "priority inheritance". If a low priority thread owns a synchonization lock, that has other threads waiting on it, it is temporary promoted to the priority of the highest priority waiting for the lock.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!