File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes threads in a distributed application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "threads in a distributed application" Watch "threads in a distributed application" New topic
Author

threads in a distributed application

Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

I am basically creating an app in RMI where a server sends message to every client that registers with it. I implemented it without creating threads and it worked. I would like to implement the same app by assigning a thread to every incoming request, but I could'nt do it.
What I basically did was whenever a request from a client came for registering with the server, I create a new thread and start it. In the run method, I call the client's display(message) method to display my message.
Now, my question is if it is possible to cater a thread to a request, such that when that thread runs it would execute the display method of only that client to which it is catered to.
What was really happening was that I merely created a new thread when a request arrived. So, if there were 4 requests(clients), there were 4 threads and any thread could execute any client's display method. I really was'nt assigning a thread to every request. If anyone has any ideas about this please help. Thank you.

Code Below for "Registering Client with server" and "Running the run method". Both are in the same class.



[ October 12, 2006: Message edited by: Allen Sylvester ]
[ October 12, 2006: Message edited by: Allen Sylvester ]
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

You need to be more specific.

What does "call the client's display(message) method to display my message" do?

Are you passing references to the started thread?

A little code would help too.


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

I am not passing any references to the started thread because I think I cannot. I might be wrong, I'm not an expert. Because, I'm starting a thread by passing a runnable.

The display(message) displays the message on the client. This method is in the client's code. The client code is a remote object and I'm passing this object to the server while I register it with the server using the registerTimeMonitor(Remote r) method
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Somehow I didn't see your code in my last post. Bad eyes on my part.

I assume that:
public void registerTimeMonitor(Remote r){
is the remote method your client is calling.

The RMI Runtime uses (create when necessary) a thread to handle each call to your remote method. Therefore, each client call will need to be synchronized since LinkedList is not synchronized. Your code may overlay elements in the list.

The sleep() in the thread is of no use.

The sync(this) in the thread is of no use since no other thread can get the same object (this refers to the current thread).

You need to synchronize on the linked list (clientList) or code from multiple thread will race.

In order for your newly created thread to ciTemp.displayTime(), forget about the LinkedList. Just pass the remote object to the new thread in place of "this" since "this" serves no purpose. In the constructor of the new thread, save what was passed in a private variable. Use that private
varialbe in place of ciTemp.
new Thread(r).start();

private ClientInterface client_obj;
client_obj.displayTime();
Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

Thanks Edward, the program works now.
Allen Bandela
Ranch Hand

Joined: Feb 16, 2006
Posts: 128

The two methods I posted above, are actually in a Runnable class. So, I cannot pass the remote object into a thread constructor.
I understand now that if the class extended thread , I could pass the remote interface to the constructor and use it to call the clients displayTime() method. If I changed my class to extend thread, its basic functionality is changed. It also has to extend UnicastRemoteObject. So, I find that threads cannot be used here to get the requirement I need, i.e to assign each incoming client request to a thread. What i can do though is create a new thread when a client calls the the registerTimeMonitor(Remote r), and make all the threads run the client's displayTime(). Basically, I can write this program without threads too. And yes, I have to synchronize the clientList. Thanks Edward.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads in a distributed application