Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Non Blocking Java Thread help

Posted on 2010-09-22
5
Medium Priority
?
470 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 40

Accepted Solution

by:
Gurvinder Pal Singh earned 500 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 1000 total points
ID: 33733479
0
 
LVL 4

Assisted Solution

by:heppa
heppa earned 500 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 1000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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…
Suggested Courses

705 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