📄 remoteclassv4.java
字号:
/*################################################################################# remoteClassv4.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 4 server" i.e.: * 1) Readers do not enter if there are writers waiting. * 2) After a writer, a waiting reader get access (just one) if there is one. * 3) The conditions under which readers or writers get access to the * resource are checked in a separate function. * 4) Other policies could be defined and checked in the proper function as * well. Also, it could be needed (for those policies) to define and use * specific local variables in order to enforce the policy (as variable * "last" in the current example). * 5) With a unique "waiting queue" for every process that should wait. * 6) Added synchronized to each remote method, to avoid race conditions. * 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 remoteClassv4 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 private int last; // Last process on the resource 0 ==> Reader, guess 1 protected remoteClassv4() 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 last = 0; // assuming last access has been for readers } // ------------------- accessReader ------------------- public synchronized Boolean accessReader() throws RemoteException { System.out.println("Entering accessReader, wwait = " + wwait + " calls = " + calls + " thread " + Thread.currentThread().getId()); calls++; // Controlling access for readers while (readerHaveToWait()) { 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++; last = 0; System.out.println("Leaving accessReader, calls = " + calls + " thread " + Thread.currentThread().getId()); return Boolean.TRUE; } // ------------------- readerHaveToWait ------------------- protected Boolean readerHaveToWait() { return ((writers > 0) || ((wwait > 0) && (last == 0))); } // ------------------- accessWriter ------------------- public synchronized Boolean accessWriter() throws RemoteException { System.out.println("Entering accessWriter, calls = " + calls + " thread " + Thread.currentThread().getId()); calls++; // Controlling access for writers while (writerHaveToWait()) { 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++; last = 1; System.out.println("Leaving accessWriter, calls = " + calls + " thread " + Thread.currentThread().getId()); return Boolean.TRUE; } // ------------------- readerHaveToWait ------------------- protected Boolean writerHaveToWait() { return ((writers > 0) || (readers > 0) || (rwait > 0) && (last == 1)); } // ------------------- leaveReader ------------------- public synchronized 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 ------------------- public synchronized 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 + -