Solved

Non Blocking Java Thread help

Posted on 2010-09-22
5
464 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 48
difference of if loops 23 49
Way to decrease size of apk file 9 67
String array comparison 4 34
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

773 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