aspose file tools*
The moose likes Distributed Java and the fly likes JOptionPane halts RMI thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "JOptionPane halts RMI thread" Watch "JOptionPane halts RMI thread" New topic
Author

JOptionPane halts RMI thread

Andrea Guarini
Greenhorn

Joined: Apr 26, 2010
Posts: 4
Hi guys, I need help about this problem: the application I'm working on is a java gestional software. I recently have implemented RMI callbacks so that the server can trigger updates on clients. After one of the clients does some modifications the server calls the refresh method of the other clients connected to refresh their opened jtables.
The situation that's giving me problem is the following:
client A opens table 1
client A views record 5
client B opens table 1
client B views record 5
client C opens table 1
client C views record 5
client A modifies record 5
server sends an update to all of the clients that currently have opened table 1. If one client has opened the record 5 the purpose of the update is to have a JOptionPane showed that asks if the user (in this case clients B and C) wants to update the visualization of record 5 with the changes made by client A or wants to keep the old version, in the case he has done some modifications he wants to commit later).

obviously if I have more clients currently looking at table 1 record 5 this JOptionPane should show on every machine.
Everytime a client connects it registers itself in an arraylist stored on the server, so when I have to call all of the clients I loop over this arraylist.
The loop on the server goes like this:

for(every client connected)
if it's not the author of the operation (in this case if it's not client A)
show JOptionPane
if the user clicks on YES
refresh()

The problem is that the loop goes on only after the previous client has decided what to do, so the JOptionPane won't show on client C until B has decided to update or not the frame and so on.
I tried modifying the previous loop so that each JOptionPane is handled by a different thread, like this:

for(every client connected)
if it's not the author of the operation (in this case if it's not client A)
{
RefreshThread d = new RefreshThread();
d.start();
}
private class RefreshThread extends Thread
{
@Override
public synchronized void start()
{
if it's not the author of the operation (in this case if it's not client A)
show JOptionPane
if the user clicks on YES
refresh()
}
}

but still no changes, the loop goes on only after the previous client has chosen to update or not.
I thought of implementing my own class to show the message, but if I create a class that shows a window with the message and two buttons (update / cancel) and set it to modal, so that the user cannot bypass it I think the thread on the server will have the same behaviour, it will wait until the modal dialog is closed.
Please give me some hints on how to correct this.
Andrea
Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 501
    
    5
Hi,

Server should invoke the client.confirmUpdate() and client.refresh() for each client on a separate thread. Also each client should show JOptionPane in the event dispatch thread context.

So the flow should be something like this:
Server:


In your current code, you're overriding the Thread.start() method in RefreshThread....this does NOT spawn a new thread and that's why you don't see any change in behaviour. The correction is to override "public void run()" in RefreshThread. The recommended way is to use a Runnable implementor and pass it to regular Thread. Thread should be subclassed only if the application is somehow changing the nature of the threading itself.

And client code:


Andrea Guarini
Greenhorn

Joined: Apr 26, 2010
Posts: 4
Thank you so much! The problem was I implemented start() instead of run().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JOptionPane halts RMI thread