Home arrow static arrow Java Programming [Archive] - Data transfer from thread to thread?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Data transfer from thread to thread?
This topic has 4 replies on 1 page.

Posts:49
Registered: 10/15/03
Data transfer from thread to thread?  
Jun 25, 2004 3:41 AM



 
Hi everyone,

I have the following problem: I have a program that has a thread that is receiving and sending data using NIO. When data arrives, the NIO thead passes the data to my main thread, who's doing a lot with it. The problem is, that the NIO thead has to wait till the data is processed (the method returns) in the main thread. How can I get arround this? I want my NIO thread to not wait on anything just pass data to somewhere and return immediately.

I was thinking of a kind of cubby hole class where the nio thread could pass the data and then notify the main thread that data is available. The only problem with this is, that I can't send my main thead to sleep with wait() since it's also responsible for the interface! Do I really need another thread, a helper thread, that waits for the data and passes it to my main thread or am I thinking totally wrong here?

Thanks for any help!

best regards,
Chris
 

Posts:10,972
Registered: 10/23/03
Re: Data transfer from thread to thread?  
Jun 25, 2004 5:14 AM (reply 1 of 4)



 
Create a queue to hold the data transferred between the threads.
The reading thread enqueues data (a simple & quick operation).
Your processing thread dequeues data to crunch it.
When the queue is empty it can either wait or do something else.
 

Posts:10,972
Registered: 10/23/03
Re: Data transfer from thread to thread?  
Jun 25, 2004 5:17 AM (reply 2 of 4)



 
Also, I just reread you comments about the so-called "main" thread being responsible for the GUI.

You're going to have to clarify that. GUI code should be executed by a EDT
(Event Dispatch Thread), and you should keep any lengthly operations out
of that thread because it will degrade response time.

Describe your thread architecture -- why can't the reading thread crunch its own data?
 

Posts:49
Registered: 10/15/03
Re: Data transfer from thread to thread?  
Jun 25, 2004 5:38 AM (reply 3 of 4)



 
Thanks for your answers, I'm trying to clarify a few things:

My programm is a dual server. It has to talk to one client on one side and to multiple clients on the other (2 different ports ;). My thread architecture is as follows: In my main class (with the main method) I'm building the guy and starting the 2 server threads. One is using normal blocking IO and is responsible for the single client (which is a billing server) and the other thread is using NIO to handle the other clients. For each client there's also a client thread which handles some external scripts which display some information remotely to the clients (the clients are just some old 486 which are only used as displays). The client threads are handled by an object in the main object.
Right now whenever I receive something from a client, I'm passing it to my main object. The main object displays the information using tables (I have my own tablemodel classes too) and then, depending on what data I got, it has to start some actions on the client threads. So the data arrives, goes to the main threads, is displayed and goes to the client thread it belongs too. And while this happens, the nio thread has to wait.

The problem is that I think that sometimes it takes to long for the whole data processing and my clients time out (10 secs). I have a timer running on the NIO thread which checks for a ping every 10 seconds and sends out a new one if the old one came back. Otherwhise the connection is closed.

Thanks for reading :)

Chris
 

Posts:10,972
Registered: 10/23/03
Re: Data transfer from thread to thread?  
Jun 25, 2004 5:51 AM (reply 4 of 4)



 
it sounds like making calls to the processing thread asynchronous (via the queue) is what you need.

Also, I don't know how careful you are with Swing. Have you read this: [url=http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html]Swing And Threads[/url]?
 
This topic has 4 replies on 1 page.