File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Synchronization issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronization issue" Watch "Synchronization issue" New topic

Synchronization issue

binayakumar patel

Joined: Jun 26, 2009
Posts: 27
I have a function which will call by more then one thread, Which is synchronized by an referance of class ClientSession. So idea behind is that the two client reqiest will process parallaly. The problem is that the wating thread are getting active randomly not the oder it called the function.

Ex: Suppose for Client 1 there are 5 threads suppose T1,T2,T3,T4,T5
When the thread T1 is executing the T2,T3,T4,T5 is wating.... When T1 is finised it task and release the lock.... then next Thread is execution randlomly.... I cant accept T2 will call.. But as per the requirement T2 will call then T3 then T4 then T5.

Please suggest me the solution.

Thanks in advance
Henry Wong

Joined: Sep 28, 2004
Posts: 20380

This sounds like this could be done better without starting your own threads. For example, instead of having 5 threads named T1, T2, T3, T4, and T5. You could design it to use one thread (or an executor that is limited to one thread), and T1, T2, T3, T4, and T5 are just runnable tasks that is placed into the queue of the executor... The executor will execute it in the correct order for you.

Now, on the other hand, if you really don't want to change your design too much, you can try the ReentrantLock class. That class supports a "fairness" policies that, when set, tries to give locks in the order encountered.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Well, that is how the synchronized blocks and wait/notify works. You won't be able to execute the tasks in order using these mechanisms.

You will probably be better off run the task in a single Thread which takes request in a Queue. Perhaps use a single threaded ThreadPoolExecutor (like one from Executors newSingleThreadExecutor() method). Then when T1 needs to call the method it submits()s a request and waits on the response (using the Future object's get() method).

Anas Alam

Joined: Dec 03, 2007
Posts: 1
try using the join method. it might help!
binayakumar patel

Joined: Jun 26, 2009
Posts: 27
Join will not help because; it will wait all other thread which is lock by object finished and then start.
But My issue is thread are not get active as the order it is waiting.
Ex-> Thread-1 lock first, then Thread-2 is waiting, after the Thread2 reached and waiting the Thread-3 comes and is waiting, similarly Thread-4 is waiting.

So I need Thread-2 will get active not Thread-3 or not Thread-4 when Thread-1 is released the lock, but it is not happening, any one of the Waiting thread is getting active and executing.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423

you can create a simple "fair" semaphore: java.util.concurrent.Semaphore
The semaphore constructor has "fainress" parameter, which when set to true guarantees the same order of releasing threads waiting on it
as the order the threads called 'acquire' method.
You must create one semaphore for each session, and in the function you could call it in this way:

You must ensure that each thread will always call release() method on the semaphore, even in case of error/exception
... if not, the other threads will hang forever
I agree. Here's the link:
subject: Synchronization issue
It's not a secret anymore!