• Post Reply Bookmark Topic Watch Topic
  • New Topic

Specific two thread synchronization  RSS feed

 
Ctirad Kr�l�k
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings

I'm working on a project which deals with converting one particullar BPEL process to Java code (it communicates with similar "BPEL services" written in Java - communication is done by Java RMI) and in the process I have 2 parallel sequences which are handled by 2 threads.

Now my task is to deal with this: to make the execution of some parts of the first thread to be conditional on an execution of some parts in the second thread. If you are familiar with BPEL, there are constructs "link", "sources" and "targets" devoted to this tasks.

I've proposed an easy way how to deal with:


I've ommited all the exception handling.

My question is really stupid: Do you think that this is a good solution? My heart told me that nothing can go wrong, but my brain refuses this thought

Thank you for each constructive answers.
[ November 17, 2008: Message edited by: Ctirad Kr�l�k ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you need is somewhat like a CyclicBarrier.
If you are on jdk 1.5 or above you get this for free or otherwise you can use the JSR 166 backport
 
Ctirad Kr�l�k
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've had a look at the CyclicBarrier, but I don't know if it's the right thing I'm looking for.

If I understood it correctly, the only way to synchronize the threads is the using of method await(). But the thing is that this causes the "faster" threads to wait until all threads call the await() method - it means that the first thread can wait for the second one or vice versa. I want only the SequenceOne thread waiting for the other... The SequenceTwo shouldn't wait for the calling await() by SequenceOne. In other words - Continuance of the SequenceOne is conditioned on actions in SequenceTwo. Not vice versa.

Nevertheless, thank you for the answer
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you are looking for the normal Lock/Condition paradigm in the Java Concurrent API:
See this Tutorial

Something like:


This assumes that SequenceOne will always get to the wait-point before SequenceTwo would get to the release point - or SequenceTwo will execute multiple times. If you can't make this assumption then you could instead use a single permission Semaphore, initialized with no available permissions. SequenceOne attempts to take a permission and SequenceTwo adds a permission. If SequenceTwo gets to the synch point first it adds a permission to the Semaphore and imediately moves on so when SequenceOne gets to that point it is allowed to take that permission and go. But if SequenceOne gets to the synch point first then it tries to get a permission, can't and so waits. Then SequenceTwo comes along, adds a permission, then moves on. When SequenceTwo adds the permission then SequenceOne comes out of sleeping and continues on:

[ November 18, 2008: Message edited by: Steve Luke ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ctirad:
Ok, I've had a look at the CyclicBarrier, but I don't know if it's the right thing I'm looking for.


I am extremely sorry for pointing you in the wrong direction. Actually, I wanted to point you to Exchanger. The reason being that it can also pass objects between threads and so it is a good option when one thread depends on the other and also needs a state context.
I dont know why i pointed you to the cyclic barrier instead. I think i need a break from work

What Steve suggested makes sense to me and sounds a good idea.
However, just looking at it differently, i have one question. Does the thread SequenceTwo do anything else after notifying SequenceOne?
If not then the easiest way would be to make SequenceOne join() with SequenceTwo after doing someAction().
 
Ctirad Kr�l�k
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, guys - thank you for the answers - they really helped me and I'll try to make the best of them

Have a nice time
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!