标签 java 下的文章

RPC和RMI的关系

RMI全称是Remote Method Invocation(远程方法调用),Java RMI威力体现在它强大的开发分布式网络应用的能力上,是纯Java的网络分布式应用系统的核心解决方案之一。其实它可以被看作是RPC的Java版本。但是传统RPC并不能很好地应用于分布式对象系统。而Java RMI 则支持存储于不同地址空间的程序级对象之间彼此进行通信,实现远程对象之间的无缝远程调用。

- 阅读剩余部分 -

在阎宏博士的《JAVA与模式》一书中开头是这样描述责任链(Chain of Responsibility)模式的:

责任链模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不影响客户端的情况下动态地重新组织和分配责任。

- 阅读剩余部分 -

定论

问:finally语句一定会执行吗?
答:

  1. 如果没有执行相应的try语句则不会执行。
  2. 在try语句中如果调用System.exit(0)方法则不会执行。

问:finally会在什么时候执行?
答:如果在try/catch语句中调用转移指令例如:return,break,continue,throw等。则会在转移指令前执行。

总结

finally与return之间的关系

如果在finally中含有return语句,那么try/catch语句的return还有作用吗?

先看一段代码:

/**
 * Created by gavin on 15-9-2.
 */
public class FinallyTest {
    public static void main(String[] args){
        System.out.println(test1());    //3
        System.out.println(test2());    //3
        System.out.println(test3());    //2
        System.out.println(test4());    //2
    }
    public static int test1()
    {
        int i = 1;
        try {
            i = 2;
            return i;
        }finally {
            i++;
            return i;
        }
    }
    public static int test2()
    {
        int i = 1;
        try {
            i = 2;
            return i;
        }finally {
            i = 3;
            return i;
        }
    }
    public static int test3()
    {
        int i = 1;
        try {
            i = 2;
            return i;
        }finally {
            i++;
        }
    }
    public static int test4()
    {
        int i = 1;
        try {
            i = 2;
            return i;
        }finally {
            i = 3;
        }
    }
}

如果你对java内存布局不是很清楚,请看这篇文章:java虚拟机类加载机制和字节码执行引擎

重点关注运行时栈帧结构(局部变量表槽操作数栈)。

上边的代码非常简单,来看一下字节码指令吧

public static int test1();
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=0
         0: iconst_1        //定义一个常量1入栈到操作数栈            
         //栈1  0:  1:
         1: istore_0        //出栈,存储到局部便量表槽0         
         //栈   0:1 1:
         2: iconst_2        //定义一个常量2入栈到操作数栈            
         //栈2  0:1 1:
         3: istore_0        //出栈,存储到局部变量表槽0         
         //栈   0:2 1:
         4: iload_0        //从局部便量表槽0入栈到操作数栈   
         //栈2  0:2 1:
         5: istore_1        //出栈,存储到局部变量表槽1         
         //栈   0:2 1:2
         6: iinc          0, 1 //局部变量表槽0变量加1               
         //栈   0:3 1:2
         9: iload_0        //从局部变量表槽0入栈到操作数栈   
         //栈3  0:3 1:2
        10: ireturn         //结束,返回                                 
        //栈3   0:3 1:2
        11: astore_2      
        12: iinc          0, 1
        15: iload_0       
        16: ireturn       
   
  public static int test2();
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=0
         0: iconst_1        //定义一个常量1入栈到操作数栈    
         //栈1  0:  1:
         1: istore_0        //出栈,存储到局部便量表槽0 
         //栈   0:1 1:
         2: iconst_2        //定义一个常量2入栈到操作数栈    
         //栈2  0:1 1:
         3: istore_0        //出栈,存储到局部变量表槽0 
         //栈   0:2 1:
         4: iload_0        //从局部变量表槽0入栈                
         //栈2  0:2 1:
         5: istore_1        //出栈,存储到局部变量表槽1 
         //栈   0:2 1:2
         6: iconst_3        //定义一个常量3入栈                 
         //栈3  0:2 1:2
         7: istore_0        //出栈,存储到局部便量表槽0 
         //栈   0:3 1:2
         8: iload_0        //从局部变量表槽0入栈                
         //栈3  0:3 1:2
         9: ireturn        //结束,返回                         
         //栈3  0:3 1:2
        10: astore_2         
        11: iconst_3      
        12: istore_0      
        13: iload_0       
        14: ireturn       
    
  public static int test3();
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=0
         0: iconst_1        //定义一个常量1入栈到操作数栈    
         //栈1  0:  1:
         1: istore_0        //出栈,存储到局部便量表槽0 
         //栈   0:1 1:
         2: iconst_2        //定义一个常量2入栈到操作数栈    
         //栈2  0:1 1:
         3: istore_0        //出栈,存储到局部变量表槽0 
         //栈   0:2 1:
         4: iload_0        //从局部变量表槽0入栈                
         //栈2  0:2 1:
         5: istore_1        //出栈,存储到局部变量表槽1 
         //栈   0:2 1:2
         6: iinc          0, 1 //局部变量表槽0变量加一       
         //栈   0:3 1:2
         9: iload_1        //从局部变量表槽1入栈                
         //栈2  0:3 1:2
        10: ireturn         //结束,返回                         
        //栈2   0:3 1:2
        11: astore_2      
        12: iinc          0, 1
        15: aload_2       
        16: athrow        
   
  public static int test4();
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=1, locals=3, args_size=0
         0: iconst_1        //定义一个常量1入栈到操作数栈    
         //栈1  0:  1:
         1: istore_0        //出栈,存储到局部便量表槽0 
         //栈   0:1 1:
         2: iconst_2        //定义一个常量2入栈到操作数栈    
         //栈2  0:1 1:
         3: istore_0        //出栈,存储到局部变量表槽0 
         //栈   0:2 1:
         4: iload_0        //从局部变量表槽0入栈                
         //栈2  0:2 1:
         5: istore_1        //出栈,存储到局部变量表槽1 
         //栈   0:2 1:2
         6: iconst_3        //定义一个常量3入栈到操作数栈    
         //栈3  0:2 1:2
         7: istore_0        //出栈,存储到局部变量表槽0 
         //栈   0:3 1:2
         8: iload_1        //从局部变量表槽1入栈                
         //栈2  0:3 1:2
         9: ireturn        //结束,返回                         
         //栈2  0:3 1:2
        10: astore_2      
        11: iconst_3      
        12: istore_0      
        13: aload_2       
        14: athrow

我们看到:

在finally中没有return时,栈中最后存储的数据是try/catch中操作后数据。即finally操作后的数据存储到其他槽中,而后再加载try/catch操作后的数据。

而在finally中含有return时,栈中最后存储的数据是finally中操作后的数据。即finally操作后的数据存储到其他槽中,而后加载的是其他槽(finally)中的数据。

也就是说:如果finally中不含有return语句,finally对try/catch操作的八大基础类型不会再加载到操作数栈中。

如果返回值是对象引用,finally中的return还有待考据。

参考:关于 Java 中 finally 语句块的深度辨析

引言

java.util.Concurrent包中,BlockingQueue很好的解决了在多线程中,如何高效安全“传输”数据的问题。通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利。同时,BlockingQueue也用于java自带线程池的缓冲队列中,了解BlockingQueue也有助于理解线程池的工作模型。

- 阅读剩余部分 -