Solved

Non Blocking Java Thread help

Posted on 2010-09-22
5
462 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now