领略分布式编程乐趣--[4]

领略分布式编程乐趣
博客提及领略分布式编程乐趣,还给出了作者邮箱、首次修改时间、个人文集及 WebLog 等信息,重点围绕分布式编程展开。
 


                     领略分布式编程乐趣

RedStar81 9/9/200313/9/2003
                                                                      
81_RedStar81@163.com
                                                                  TomHornson.student@www.sina.com.cn

首次修改时间:13/9/2003
            
个人文集:
http://www.youkuaiyun.com/develop/author/netauthor/RedStar81/
             WebLog: http://www.advogato.org/person/TomHornson/

 

   

2)访问远程对象

我们知道在RMI或是Remoting,使用分布式对象技术,你需要建立服务器端的远程对象,这个对象需要实现Remote接口并且继承于UnicastRemoteObject以获得分布式特性支持。而这一切在Voyager下变得简单了。Voyager在运行时生成一个类型兼容的代理,并且提供了几种方法做到这一点: a 创建一个本地对象,然后调用Proxy的静态方法Proxy.of(Object obj)来创建它的代理.  b 使用名字服务。Voyager提供名字服务功能。每个对象都可以通过将它们的名字服务登记到名字服务中,然后其它程序就可以通过Namespace.lookup(String Name)来得到这个对象的一个代理。 c 这个方法很有特色。在客户端你可以通过Factory.create(String classname,String url)在远程机器上建立对象,并且返回远程对象的代理。

下面我们就通过这几种方法实践一下Voyager环境下远程对象计算:

 

方法A示例:

程序片断1:

//

//Project : RemoteObject

//Filename : IBall.java

//Creator And Date : RedStar81 2003-8-22 22:51

//

//Statement :

//

 

public interface IBall{

  public void hit();

}

  

   //

//Project : RemoteObject

//Filename : Ball.java

//Creator And Date : RedStar81 2003-8-22 22:51

//

//Statement :

//

 

public class Ball implements IBall{

 

public void hit(){

    System.out.println("Ball has been hit");

}

}

 

//

//Project : RemoteObject

//FileName : Bat.java

//Creator And Date : RedStar81 2003-8-22 22:50

//

//Statement :

//

 

import com.objectspace.voyager.*;

 

public class Bat{

 

public void play(IBall iball){

    iball.hit();

}

 

public static void main(String[] args){

 

try{

    Voyager.startup();

   

    Bat bat = new Bat();

    IBall iball = (IBall)Namespace.lookup("8000/Ball");   

    bat.play(iball);   

}catch(Exception e){

       System.err.println(e);

}

    Voyager.shutdown();

}

 

}

 

//

//Project : RemoteObject

//Filename : BallMachine.java

//Creator And Date : RedStar81 2003-8-22 22:50

//

//Statement :

//

 

 

import com.objectspace.voyager.*;

 

public class BallMachine{

 

public static void main(String[] args){

 

try{

    Voyager.startup("8000");

    Ball ball = new Ball();

    IBall iball = (IBall)Proxy.of(ball);

    Namespace.bind("8000/Ball",iball);

}catch(Exception e){

       System.err.println(e);

}

 

}

}

上面的程序很简单,略知RMI或时Remoting,看起来一目了然,这里就不再讲解了.

 

 

程序片断二:下面是方法一复杂一点点的程序,这里客户端在调用远程对象的方法时,传递计算对象:

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : IComputing.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

public interface IComputing{

public Double computing(ComputingObject co);

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : Computing.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

 

public class Computing implements IComputing{

 

public Double computing(ComputingObject co){

   

    System.out.println("Computing is done on this machine !");   

    return co.workCode();

}

 

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : ComputingServer.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

import java.io.*;

 

public class ComputingObject implements Serializable {

//

//Serializable : need !

//

private double sum = 0;

private int dtStart,dtEnd;

 

public ComputingObject(int dataStart,int dataEnd){

    dtStart = dataStart;

    dtEnd = dataEnd;

}

   

public Double workCode()

{

       for(int control = dtStart ; control < dtEnd ; control++)

              sum += Math.sqrt(control);

       return new Double(sum);

}

 

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : ComputingServer.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

import com.objectspace.voyager.*;

 

public class ComputingServer{

 

public static void main(String[] args)

{

      

try{

    Voyager.startup("8000");

    Computing computingObject = new Computing();

    IComputing iComputing = (IComputing)Proxy.of(computingObject);

    Namespace.bind("8000/Computing",iComputing);

}catch(Exception e)

{

       System.err.println(e);

}   

 

}

}

 

//

//Project : RemoteObjectComplexComputing using voyager producted by objectspace

//Filenaem : Caller.java

//Creator And Date : RedStar81 2003-8-23 0:07

//

//Statement : It is uesd to test the conditons when pass a object !

//

 

import com.objectspace.voyager.*;

 

public class Caller{

 

public static void main(String[] args){

      

try{

    Voyager.startup();

    Computing computingObject = new Computing();

   

    IComputing iC = (IComputing)Namespace.lookup("//localhost:8000/Computing");

   

    double returnValue = (iC.computing(new ComputingObject(1,100000))).doubleValue();

    System.out.println(returnValue);   

 

}catch(Exception e)

{

       System.err.println(e);

}

    Voyager.shutdown();  

}

}

上面需要注意的就是传递给远程对象方法的参数需要实Serializable接口.

 

 

待续:

 

2.    DCOM

3.    CORBA

4.       WebServices

5.         Sun RMI  VS. dotNET Remoting

6.         LindaSun JavaSpace ,IBM Tspace

7.         JINI

8.         Emerald VS. Dejay

9.         Pjama

10.     IBM Mobile Computing Interface : Aglets

.综合应用分析

1.       利用多线程和分布对象计算技术模拟高性能并行计算

2.       利用Vdejay计算几何分形图

 

 

  声明:

1。本人不是专门研究分布式的,完全出于兴趣写下这篇文章.希对分布式计算应用爱好者有所帮助。
2
。水平有限,欢迎指正。
3
。由于本文讲述内容较多,很多讲述不可能太细致。而且考虑到读者群的问题,理论性内容尽量的省略。
  
如果读者有兴趣,可参考列出的参考书籍和网络资源。

4。可任意的转载、不过请注明出处;不可用于商业用途。

 

在C语言里,位左对齐右对齐一般在格式化输出时会用到,主要用于控制数据在输出时的位置。以下是相关介绍: ### 整型数据的左对齐右对齐 通过`printf`函数实现整型数据的左对齐右对齐右对齐是默认方式,在格式说明符`%`和`d`之间添加数字来规定输出宽度,若数字位数小于规定宽度,会在左边补空格;左对齐则需在数字前加`-`号,若数字位数小于规定宽度,会在右边补空格。 示例代码如下: ```c #include <stdio.h> int main() { // 右对齐。数字宽度为10,若不足10,在左边补足空格 printf("%10d\n", 1234); // 左对齐。数字宽度为10,若不足10,在右边补足空格 printf("%-10d\n", 1234); return 0; } ``` ### 不同输出长度的情况 当规定的输出宽度和数字实际位数不同时,有不同的处理方式。若规定宽度小于数字实际位数,会完整输出数字;若规定宽度大于数字实际位数,右对齐在左边补空格,左对齐在右边补空格。 示例代码如下: ```c #include <stdio.h> int main() { // -5是左对齐,输出长度为5。5是右对齐,输出长度为5 printf("%-5d %5d\n", 455, 455); printf("%-5d %5d\n", -123, -123); // 规定宽度小于实际位数,完整输出数字 printf("%-5d %5d\n", 987654, 987654); return 0; } ``` ### 其他数据类型的对齐 除整型外,其他数据类型也能实现左对齐右对齐。例如浮点数(`%f`)、字符串(`%s`)等,方法和整型一致。 示例代码如下: ```c #include <stdio.h> int main() { // 右对齐浮点数,宽度为10 printf("%10f\n", 3.14); // 左对齐浮点数,宽度为10 printf("%-10f\n", 3.14); // 右对齐字符串,宽度为10 printf("%10s\n", "hello"); // 左对齐字符串,宽度为10 printf("%-10s\n", "hello"); return 0; } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值