2012-07-30 50 views
1

我很難理解哪個線程執行特定的方法。服務器端是否有任何方法?我是RMI的新手。RMI:線程是如何創建的?

HelloClient:

public class HelloClient 
{ 
    Random rand = new Random(); 

    public static void main(String[] args) 
    { 
     Thread.currentThread().setName("Thread of a client"); 
     if(System.getSecurityManager() == null) 
     { 
      System.setSecurityManager(new RMISecurityManager()); 
     } 

     HelloClient hc = new HelloClient(); 
     hc.methodToMeasureEnglish(); 
    } 


    void methodToMeasureEnglish() 
    { 
     try 
     { 
      Thread.sleep(Math.abs(rand.nextInt()%4000)); 
      Registry reg = LocateRegistry.getRegistry("localhost", 6666); 
      HelloIF hello = (HelloIF) reg.lookup("HELLO"); 
      System.out.println(hello.sayHelloEnglish()); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

您好:

public class Hello extends UnicastRemoteObject implements HelloIF 
{ 
    public Hello(String name) throws RemoteException 
    { 
     try 
     { 
      Registry registry = LocateRegistry.createRegistry(6666); 
      registry.rebind(name, this); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 


    public String sayHelloEnglish() 
    { 
     return "GOOD MORNING"; 
    } 
} 

HelloIF

public interface HelloIF extends Remote 
{ 
    public String sayHelloEnglish() throws RemoteException; 
} 

爲HelloServer

public class HelloServer 
{ 
     public static void main(String[] args) throws Exception 
     { 
      Thread.currentThread().setName("Server Thread"); 
      if(System.getSecurityManager() == null) 
      { 
       System.setSecurityManager(new RMISecurityManager()); 
      } 
      Hello myObject = new Hello("HELLO"); 
      System.out.println("Server is ready..."); 
     } 

} 

我在使用RMI嗎?

我添加的AspectJ類

@Aspect 
public class MeasureAspect 
{ 
    private static Logger logger = Logger.getLogger(MeasureAspect.class); 

    @Around("call(void method*())") 
    public Object condition2(ProceedingJoinPoint joinPoint) throws Throwable 
    { 
     PropertyConfigurator.configure("log4j.properties"); 
     Object res = joinPoint.proceed(); 
     logger.info("Thread method " + Thread.currentThread().getName());  
     return res; 
    } 

    @Around("call(String say*())") 
    public Object condition1(ProceedingJoinPoint joinPoint) throws Throwable 
    { 
     PropertyConfigurator.configure("log4j.properties"); 
     Object res = joinPoint.proceed(); 
     logger.info("Thread say " + Thread.currentThread().getName()); 
     return res; 
    } 

} 

所有日誌來自客戶端線程。你能解釋我女巫線程執行我的方法嗎?

回答

1

RMI規範說你不能對任何線程遠程方法執行做任何假設。具體而言,這意味着幾件事情:

  1. 你不能認爲它是單線程的。
  2. 您不能假定來自同一客戶端線程的連續調用將在同一個服務器線程中執行。
  3. 你不能假設它將全部在主線程中執行,而不是任何意義。

在實踐中,受線程,如果在任何服務器(甲骨文的JVM不這樣做,但我相信IBM的那樣)池,和/或收集在客戶端池(甲骨文的JVM做的,是不是瞭解IBM),每個方法調用都在其自己的線程上執行。