Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

Non Blocking Java Thread help

I am having trouble understanding how to complete my task.

I have a class that starts a thread that contains a queue of actions. When the thread processes these actions there will be times that the given action requires additional input from the class that started the thread. What I cannot figure out is how I do the following:

Class A starts Thread T
T starts and begins to process action queue
T  comes across an action that requires additional input from A
T pauses waiting for A to post additional info
A Posts info to T
T sees post and continues processing action queue with additional info.

What I cannot figure out is how to make T pause, non-blocking, waiting for A to post. Then A invokes a method within T to post the additional info and continue on. Basically I need to create some type of non-blocking loop so that methods within T can still be called by A but T waits to process the queue until A post more info.

Hope you can help!!!!
0
pHOdAT
Asked:
pHOdAT
4 Solutions
 
Gurvinder Pal SinghCommented:
you first need to know how to make an asyncronous (non-blocking) method call
http://cephas.net/blog/2004/02/02/asynchronous-method-invocation-in-java/

another way to set up a method call later is futuretask
http://programmingexamples.wikidot.com/futuretask

You can make a thread wait on condition by
http://stackoverflow.com/questions/289434/how-to-make-a-java-thread-wait-for-another-threads-output

0
 
ksivananthCommented:
0
 
heppaCommented:
Hi!

I think, we would need some more information about the type of "post" by A.

but one quick idea would be a wait on a monitor object that both Thread instances and A share (and that could also hold the "post" by A), then calling a method to set the value in T from A and then do a notify. Not sure yet, if that would do what you want.

http://download.oracle.com/javase/tutorial/essential/concurrency/

reading this would for sure help!

You could also think about "Queues" (may be overkill here)

btw: you can see this like interaction between threads, since you know that instance of Class A and instance of Thread T are in different Threads.

http://www.javamex.com/tutorials/synchronization_producer_consumer.shtml

Good luck!
0
 
ksivananthCommented:
hope you understand how to create Condition object from Lock, here is how T waits for A's input,

lock.lock() ;
try{
      try{
            yourConditionObj.await() ;
      }catch( InterruptedException ie ){}
}finally{
      lock.unlock() ;
}

here is how A notifies T,

lock.lock() ;
try{
      yourConditionObj.signal() ;
}finally{
      lock.unlock() ;
}

Note: await call should be in a loop to avoid premature awake!
0
 
pHOdATAuthor Commented:
I can honestly say that  I received great information from all of you and i couldn't decided on a single  solution so I split it amongst you all. Thanks and having used the links and code posted here I was able to get my solution to work .Thanks!!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now