Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronization between Remote and GUI  RSS feed

Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings all, first of all, thanks to all those that answered my previous post
on this thread

I'm running a multi-threaded server and sockets

inside my run method, after sending along the session id to the remote
I enter a do while (true), and wait for a object to be written from the
remote to the server.

My question is this, I'm trying to idle the Remote thread (in the Remote's
run method) until it is alerted that the the GUI is giving the Remote an object to sent the Server, at that point I want the Remote to wake up, grab the object and sent it onto the Server, the Server then processess the Sent Object does its thing and sends an Object back to the Remote. I keep getting tied up in circular references and deadlocked threads, I am trying to use the Observable Pattern but I just can't seem to get it right, can someone please point me in the right direction on how to use the Observer Pattern between two threads?

Thanks, in advance,

[Andrew: Put code between [code] and [/code] UBB Tags]
[ July 01, 2005: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander
Posts: 12148
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,

First a big word of warning: you need to be very careful when creating separate threads running at the same time as your GUI. It is very easy to get it wrong.

I have created some demonstration code below that shows a thread on the client side waiting until there is a message to send, then notifyObservers when the response is received back from the server. I think this meets both of your specified requirements.

However this code allows the user to send a second message while the server is still processing the first message - this may not be desirable.

There are a lot of concepts in this example, and I am supposed to be doing something else at the moment so I have not put any comments in here. Please just take a look at it, and ask questions about anything that doesn't make sense.

First the server code:Then the Client code:It should be noted that I used JDK 5 to develop this code - this allows me to use the JFrame.add methods to add components directly, rather than getting the contentPane.

Note that I have added a delay between 10 and 15 seconds before the server responds - this is so that you can experiment with what happens if two messages are sent.

Hopefully I will see some questions from this.

Regards, Andrew
[ July 01, 2005: Message edited by: Andrew Monkhouse ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!