Solved

Non Blocking Java Thread help

Posted on 2010-09-22
5
466 Views
Last Modified: 2012-08-13
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
Comment
Question by:pHOdAT
5 Comments
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 125 total points
ID: 33733466
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
ID: 33733479
0
 
LVL 4

Assisted Solution

by:heppa
heppa earned 125 total points
ID: 33733521
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
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 250 total points
ID: 33733567
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
 

Author Closing Comment

by:pHOdAT
ID: 33761666
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question