Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronization issue

 
binayakumar patel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Binaya
 
Henry Wong
author
Marshal
Pie
Posts: 21398
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using the join method. it might help!
 
binayakumar patel
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic