As soon as you click the "Start" button, the program calls the dangerousLoop() method of remote object. Now, if you click the "Exit" button, the application will not interact until the dangerousLoop() method finishes the loop and returns. Run the program and see the result.

Figure 10.1



In the next example, I will show you how to make a responsive user interface.

The source code needed for this example is stored in the examples\Multithread directory. The "Multithread" directory contains the following files:

  • MultiThreadInterface.java
  • MultiThreadImpl.java
  • MultiThreadClient.java
  • Compile.bat
  • Rmicompile.bat
  • RunServer.bat
  • RunClient.bat


Universal TeacherUsing Threads

The simplest way to create a new thread is to inherit from the java.lang.Thread class. In Java, every thread corresponds to an instance of the Thread class. The most important method for Thread is run(). The code in the run() method is executed simultaneously with other threads. You should call the start() method in your object to start the thread.

If you want the thread to execute the run() method of some object other than itself, you can implement the Runnable interface. You still need to construct an instance of the Thread class.

Remote Interface

Listing 10.4 MultiThreadInterface

import java.rmi.*;

//remote interface
public interface MultiThreadInterface 
	extends Remote	{
	
    //remote method
    public String dangerousLoop()
	throws RemoteException;
}

The remote interface declares only one method that returns a String object.

 

 

 

RMI BOOK MAIN PAGE                Top

  
Copyright © 2001 www.universalteacher.com