Results 1 to 3 of 3
  1. #1
    cornercuttin is offline Member
    Join Date
    Jan 2008
    Rep Power

    Default data from the main/GUI thread to another runnin thread...

    hello all,

    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.

  2. #2
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Rep Power


    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 @ []
    Language is froth on the surface of thought

  3. #3
    xfrag's Avatar
    xfrag is offline Member
    Join Date
    Apr 2008
    Athens, Greece
    Rep Power


    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.

Similar Threads

  1. Replies: 0
    Last Post: 01-28-2008, 07:02 AM
  2. main thread question?
    By frejon26 in forum New To Java
    Replies: 1
    Last Post: 01-24-2008, 10:13 PM
  3. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts