Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Synchronization issue

 
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
 
author
Posts: 23844
141
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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).
 
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.
 
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
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!