📄 fabnaplet.java
字号:
/* * @<#> FabNaplet.java version 0.0.1, 1/1/2000 * * THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR * MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION. * * 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. * * Copyright (c) 2000 Wayne State University. All Rights Reserved. */package fabonacci;/** * The <code>FabNaplet</code> class implements a naplet example * that calculates a fabonacci function in multithreading on a remote server. * * @version 0.1, 12/1/2001 * @author C. Xu * */import java.io.*;import java.util.*;import naplet.*;import naplet.itinerary.*;public class FabNaplet extends Naplet implements Runnable{ /** * Constructs a naplet * @param servers a list of servers to be visited * @param listener listener of the naplet waiting for results * @size fabonacci function size */ public FabNaplet( String[] servers, NapletListener listener, int size ) throws InvalidNapletException, InvalidItineraryException { this( null, servers, listener, size ); } public FabNaplet( String name, String[] servers, NapletListener listener, int size ) throws InvalidNapletException, InvalidItineraryException { super( name, listener ); // Create a naplet state space to keep results, including // size and Fabonacci list setNapletState( new ProtectedNapletState() ); Integer Size = new Integer( size ); ArrayList fabList = new ArrayList(); try { getNapletState().set( "size", Size ); getNapletState().set( "result", fabList ); } catch ( NapletStateAccessException nsae ) { throw new InvalidNapletException( "FabNaplet state initialization failure" ); } setItinerary( new ICItinerary( servers ) ); } /** * Entry point of a naplet at each server */ public void onStart() throws InterruptedException { String serverName; serverName = getNapletContext().getServerURN().getHostName(); System.out.println( "Naplet " + this.getNapletID().toString() + " arrives at " + serverName ); // Calculation of Fabonacci list can be run in multi-threads Thread thr = new Thread( this ); thr.start(); thr.join(); try { getItinerary().travel( this ); } catch ( NapletMigrateException nme ) { throw new NapletRuntimeException( nme.getMessage() ); } } public void run() { try { Integer Size = ( Integer ) getNapletState().get( "size" ); ArrayList fabList = ( ArrayList ) getNapletState().get( "result" ); fabonacci( Size.intValue(), fabList ); } catch ( NapletStateAccessException nsae ) { throw new NapletRuntimeException( nsae ); } catch ( InterruptedException ie ) { // Be prepared to catch interrupt during the process of calculation onInterrupt(); } } private void fabonacci( int size, ArrayList list ) throws InterruptedException { int lo = 1; int hi = 1; list.add( new Integer( lo ) ); while ( hi < size ) { list.add( new Integer( hi ) ); hi = lo + hi; lo = hi - lo; } } public void onInterrupt() { System.out.println( "I'm interrupted" ); try { getItinerary().travel( this ); } catch ( NapletMigrateException nme ) { System.out.println( nme.getMessage() ); } } static void classFinalize() { System.out.println( "MyNaplet was finalized" ); } /** * The <code>ResultReport</code> class defines an operation to be performed * at the end of an itinerary. */ private class ResultReport implements Operable { public void operate( Naplet nap ) { try { ArrayList fabList = ( ArrayList ) getNapletState().get( "result" ); if ( nap.getListener() != null ) { nap.getListener().report( fabList ); } } catch ( java.rmi.RemoteException re ) {} catch ( NapletStateAccessException nsfe ) { throw new NapletRuntimeException( nsfe ); } } } private class ICItinerary extends Itinerary { public ICItinerary( String[] servers ) throws InvalidItineraryException { Operable act = new ResultReport(); // seq(s0,s1,s2) setRoute( new SeqPattern( servers, act ) ); System.out.println( "ICItinerary = " + getRoute().toString() ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -