📄 remoteclassv1.java
字号:
/*################################################################################# remoteClassv1.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################################################################################ * * This is "Version 1 server" i.e.: * 1) No priorities. * 2) With a unique "waiting queue" for every process that should wait. * 3) Added synchronized to each remote method, to avoid race conditions. * Used from startService, which creates and registers one object of * this class. * Note that keyword "synchronized" is added to each remote method and * this does not violates the specification, since "synchronized" is not * part of the method's signature. * Used from startService, which creates and registers one object of * this class. */import java.rmi.RemoteException; // For rmiimport java.rmi.server.UnicastRemoteObject; // For rmiimport java.lang.Thread; // For reporting thread idspublic class remoteClassv1 extends UnicastRemoteObject implements remoteMethod{ private int calls; // Just to count the number of rmi(nvocations) private int readers, writers; // Number of each kind of process currently accessing private int rwait, wwait; // Number of each kind of process currently waiting // rwait and wwait are "maintained" but not used protected remoteClassv1() throws RemoteException { super(); calls = 0; // no service provided so far readers = 0; // no readers accessing rwait = 0; // no readers waiting access writers = 0; // no writers accessing wwait = 0; // no writers waiting access } // ------------------- accessReader ------------------- synchronized public Boolean accessReader() throws RemoteException { System.out.println("Entering accessReader, calls = " + calls + " thread " + Thread.currentThread().getId()); calls++; // Controlling access for readers while (writers > 0) { rwait++; try { wait(); } catch(IllegalMonitorStateException e) { System.out.println("Exception IllegalMonitorStateException on accessReader on wait()"); e.printStackTrace(); return Boolean.FALSE; } catch(InterruptedException e) { System.out.println("Exception InterruptedException on accessReader on wait()"); e.printStackTrace(); return Boolean.FALSE; } rwait--; } // This reader gets the access readers++; System.out.println("Leaving accessReader, calls = " + calls + " thread " + Thread.currentThread().getId()); return Boolean.TRUE; } // ------------------- accessWriter ------------------- synchronized public Boolean accessWriter() throws RemoteException { System.out.println("Entering accessWriter, calls = " + calls + " thread " + Thread.currentThread().getId()); calls++; // Controlling access for writers while ((writers > 0) || (readers > 0)) { wwait++; try { wait(); } catch(IllegalMonitorStateException e) { System.out.println("Exception IllegalMonitorStateException on accessWriter on wait()"); e.printStackTrace(); return Boolean.FALSE; } catch(InterruptedException e) { System.out.println("Exception InterruptedException on accessReader on wait()"); e.printStackTrace(); return Boolean.FALSE; } wwait--; } // This writer gets the access writers++; System.out.println("Leaving accessWriter, calls = " + calls + " thread " + Thread.currentThread().getId()); return Boolean.TRUE; } // ------------------- leaveReader ------------------- synchronized public Boolean leaveReader() throws RemoteException { System.out.println("Entering leaveReader, reader = " + readers + ", wwait = " + wwait); // This reader doesn't access anymore readers--; try { // wakeup every body else waiting notifyAll(); } catch(IllegalMonitorStateException e) { System.out.println("Exception IllegalMonitorStateException on leaveReader on notifyAll()"); e.printStackTrace(); return Boolean.FALSE; } System.out.println("Leaving leaveReader"); return Boolean.TRUE; } // ------------------- leaveWriter ------------------- synchronized public Boolean leaveWriter() throws RemoteException { System.out.println("Entering leaveWriter"); // This writer doesn't access anymore writers--; try { // wakeup every body else waiting notifyAll(); } catch(IllegalMonitorStateException e) { System.out.println("Exception IllegalMonitorStateException on leaveWriter on notifyAll()"); e.printStackTrace(); return Boolean.FALSE; } System.out.println("Leaving leaveWriter"); return Boolean.TRUE; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -