Newer
Older
NWP / src / j4 / PiImpl.java
@Motoki Miura Motoki Miura on 22 Sep 2020 1 KB first commit for NWP exp
package j4;

// RMIによる分散処理プログラムの実装例
// (2)リモートサービスの実装のクラスファイル

// PiImpl.java
// このクラスは、リモートサービスの実装クラスです
// 分散処理システムにおける機能を記述します
// 具体的には、モンテカルロ法によりπ/4を計算します

// ライブラリの利用
import java.rmi.RemoteException ;
import java.rmi.server.UnicastRemoteObject ;
import java.rmi.server.ServerNotActiveException ;
import java.lang.Math ;

// PiImplクラス
public class PiImpl extends UnicastRemoteObject	implements Pi{
    
    // コンストラクタPiImpl
    public PiImpl() throws RemoteException{
	super();
    }
    
    // putPiメソッド
    // モンテカルロ法によりπ/4を計算します
    public long putPi(long maxloopcount) throws ServerNotActiveException {
	String client = getClientHost();
	System.out.println( "PiImpl putPi is called from "+client+". ("+ maxloopcount+" times)");
	long i ; // 点生成の繰り返し回数
	long in=0 ; // 半径1の円内に点が存在する場合の数
	double x,y ; // ランダムに生成する点のx,y座標
	for(i=0 ; i<maxloopcount ; i++){
	    x=Math.random() ;// x座標
	    y=Math.random() ;// y座標
	    if ((x*x+y*y)<=1.0) {// もし半径1の円内ならば・・・
		in++ ; // inを1増やす
	    }
	}
	System.out.println( "putPi ended. return value (in) is "+in);
	return in ;
    }
}