Client ImplementationI will make this program more responsive by running the code that calls the remote method in a separate thread. Now we will use two threads, one for calling the dangerousLoop() method, and the other that takes care of the user interface. Listing 9.6 MultiThreadClient (Complete client code)import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.rmi.*; //client implementation public class MultiThreadClient extends JFrame{ //instance variables private JTextArea txtMessage; private JButton btnStart,btnExit; private MultiThreadInterface remote; //constructor public MultiThreadClient(){ super("Multiple Threads"); //get a content pane object Container cp = getContentPane(); JPanel btnPanel = new JPanel(); //text area used to show message txtMessage = new JTextArea(8,25); //button to call remote method btnStart = new JButton("Start"); //button to exit application btnExit = new JButton("Exit"); //add action listener to buttons. ButtonHandler handler = new ButtonHandler(); btnStart.addActionListener(handler); btnExit.addActionListener(handler); //add components to containers btnPanel.add(btnStart);btnPanel.add(btnExit); cp.add(txtMessage); cp.add(btnPanel,BorderLayout.SOUTH); try{ |
||
|
||
Copyright © 2001 www.universalteacher.com |
||