Results 1 to 3 of 3
- 01-29-2008, 09:29 PM #1Member
- Join Date
- Jan 2008
- Rep Power
data from the main/GUI thread to another runnin thread...
it has been a long, long time since i have done java work, and even longer since i have done threading work in java, so i am a little rusty, and after a few hours of research and tinkering, i can't seem to figure something out (this would be easy with pointers...).
basically, i am writing some software that has a C based background process, but with a Java front end (don't ask why. an existing code-base made it a necessity). the java GUI is extremely simple; basically just drop down boxes, in which the values were being written to text files and picked up by the C. now we are going to get rid of the text files and go with sockets.
basically, the java only needs 2 threads. the GUI thread and then the thread running the socket stuff. the socket thread is running as a server, and the C will be running as a client. this is how it had to work because this is basically an extension to the C code, and it cannot spawn another thread, nor sit in a wait state forever.
all i need to know is how to continuously pass information from the GUi thread to the socket thread. basically, there are a few objects that the data resides in (the data that is being entered by the GUI), and i need to pass this stuff to the socket thread as it is updated (every time a drop down box is clicked, the updated data needs to go to the socket thread).
this is an interesting concept, considering the socket thread is in a "wait" state, meaning the line "Socket connectionSocket = myServerSocket.accept();" puts this thread in a "wait" state as is. so data in the socket thread isn't getting updated as it should; it is only getting updated when it receives another connection and finally gets out of that "wait" state.
anyone have any clues? if you had a Java Server running and in its "wait" state, how do you dynamically update information that it is supposed to send to the client?
thanks a ton for any help whatsoever.
- 04-23-2008, 06:38 PM #2
You could instantiate an Event (you have to define the class) containing the status of the GUI every time the GUI changes, on the GUI thread. The same GUI thread will store these events in a queue of events (synchronized). When the socket thread wakes up will process the events in the queue. On the socket thread you can implement whatever policy for processing you want - process the whole history or only the last state, etc. Hope it helps, I am not sure I got the problem right.
Last edited by danielstoner; 04-23-2008 at 10:37 PM.Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 04-23-2008, 10:30 PM #3
If I got it right, you want to have a Java GUI to control your C application, right?
But I didn't understood if you start the two processes separately (start the Java process witch waits in server mode and then start the C process with connects to the Java process as a client) or the one starts the other (C process starts the Java process and then connects to it as a client).
If the second case best describes what you're trying to do, then you should consider using JNI. As you're familiar with C, you should not have a problem to follow the instructions described in this book (chapter 7 - "The Invocation Interface" shows how to start the JVM) and get rid of sockets and synchronization issues completely.
I've done exactly the same thing to pass events triggered by a Java process to a native application. If you decide to follow this path I can help you by posting here parts of the code I've already written.
- By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM
- By frejon26 in forum New To JavaReplies: 1Last Post: 01-24-2008, 10:13 PM
- By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM