aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Synchronization between Remote and GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Synchronization between Remote and GUI" Watch "Synchronization between Remote and GUI" New topic
Author

Synchronization between Remote and GUI

Thomas Paul Bigbee
Ranch Hand

Joined: Jun 28, 2005
Posts: 71
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,
Tom



[Andrew: Put code between [code] and [/code] UBB Tags]
[ July 01, 2005: Message edited by: Andrew Monkhouse ]
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11479
    
  94

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 ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronization between Remote and GUI