⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 askremote.java

📁 solve readers-writers problem with RMI
💻 JAVA
字号:
/*################################################################################# askRemote.java## Copyright (C) 2007 Fernando G. Tinetti## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA################################################################################ * * askRemote.java *    Expected to be used as a "client", behaving as a reader *    process or a writer process depending upon the "command *    line argument".  *    Access the resource by 5 secs. (hardcoded), this is the  *    elapsed time between the return from access...() and the *    call to leave...()  *    Used with  *               java askRemote <hostname> <Read/Write> *       <hostname>: hostname or IP of the host where the remote *                   object ("server") is "waiting" *       <Read/Write>: Read  ==> reader process *                     Write ==> writer process */import java.rmi.Naming;                    /* lookup         */import java.rmi.registry.Registry;         /* REGISTRY_PORT  */import java.net.MalformedURLException;     /* Exceptions...  */import java.rmi.NotBoundException;import java.rmi.RemoteException;import java.lang.Thread;                   /* Just to sleep  */public class askRemote{  public static void main(String[] args)  {    System.out.println("Arguments received:");    for (String s: args) {      System.out.println("   " + s);    }    /* Look for hostname and msg length in the command line */    if (args.length != 2)    {      System.out.println("2 arguments needed: (remote) hostname [Read/Write]");      System.exit(1);    }    try {      String rname = "//" + args[0] + ":" + Registry.REGISTRY_PORT + "/remote";      // System.out.println("About to lookup for " + rname);      remoteMethod remote = (remoteMethod) Naming.lookup(rname);      if (args[1].compareTo("Read") == 0)      {        System.out.println("About to call accessReader");        remote.accessReader();        System.out.println("Reading...");        Thread.currentThread().sleep(5000);        System.out.println("About to call leaveReader");        remote.leaveReader();        System.out.println("Back from leaveReader");        }      else if (args[1].compareTo("Write") == 0)      {        remote.accessWriter();        System.out.println("Writing...");        Thread.currentThread().sleep(5000);        remote.leaveWriter();      }      else      {        System.out.println("2 arguments needed: (remote) hostname [Read/Write]");        System.exit(1);      }      System.out.println("Done");    } catch (MalformedURLException e) {        e.printStackTrace();    } catch (RemoteException e) {        e.printStackTrace();    } catch (NotBoundException e) {        e.printStackTrace();    } catch (Exception e) {        e.printStackTrace();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -