上善若水
In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
posts - 146,comments - 147,trackbacks - 0

为什么会抛出IllegalMonitorStateException?

在使用java线程的时候,我们有时候要调用wait,notifyAll,notify来等待或者唤醒线程,如果这几个方法没有包含在synchronized块中,将抛出IllegalMonitorStateException异常,并且当前线程被中断,为什么?

为什么?因为wait,notifyAll,notify被调用的时候,都要使用到对象的监视器(锁),但是,如果这些方法不被包含在synchronized块中,那么当前线程就获取不到对象的锁,那么当我们wait的时候,wait根本不知道该释放哪个锁,所以就会抛出不合法的锁异常。

为什么?sleep不需要 被包含在synchronized块中呢?因为sleep不要释放锁,所以也就不抛出异常。

"Could not resolve placeholder"解决方案

  除去properites文件路径错误、拼写错误外,出现"Could not resolve placeholder"很有可能是使用了多个PropertyPlaceholderConfigurer或者多个<context:property-placeholder>的原因。
在Spring 3.0中,可以写:
  1. <context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />  
在Spring 2.5中,<context:property-placeholder>没有ignore-unresolvable属性,此时可以改用PropertyPlaceholderConfigurer。其实<context:property-placeholder location="xxx.properties" ignore-unresolvable="true" />与下面的配置是等价的
  1. <bean id="随便" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  2.     <property name="location" value="xxx.properties" />  
  3.     <property name="ignoreUnresolvablePlaceholders" value="true" />   
  4. </bean>  

用Spring JMS,在主线程退出后,进程没有退出情况:
今天写了一个关于Spring JMS的小程序,发现主线程退出后,但相应的Server进程却没有退出。用jconsole查看内部线程情况,发现还有好多线程并没有结束。如图:
在网上没有找到相关的资料,无意中看到貌似可以通过ClassPathXmlApplicationContext中的close()方法解决这个问题。对Spring和JMS其实都不算很了解,不知道这个方法是不是合适或者还有更好的方法,先记下,等以后有时间再好好研究研究。


Spring获取插入数据库时自增字段的值:
代码如下:
public int insertSubscriberRecord(int websiteId, 
                                  String firstName, 
                                  String lastName, 
                                  String password, 
                                  String email)
{
  Subscriber subscriber 
= new Subscriber(websiteId, firstName, lastName, password, email);
  String insertFileString 
= "INSERT INTO subscribers "
    
+ "(website_id, first_name, last_name, password, email_address) VALUES "
    
+ "(:websiteId, :firstName, :lastName, :password, :emailAddress) ";
  
// see http://static.springframework.org/spring/docs/2.5.x/reference/jdbc.html
  SqlParameterSource fileParameters = new BeanPropertySqlParameterSource(subscriber);
  KeyHolder keyHolder 
= new GeneratedKeyHolder();
  getNamedParameterJdbcTemplate().update(insertFileString, fileParameters, keyHolder);
  
return keyHolder.getKey().intValue();
}

参考:http://www.devdaily.com/blog/post/jdbc/spring-jdbc-insert-auto-generated-key

Print the Stack Trace of the Exception to a String
 1import java.io.PrintWriter;
 2import java.io.StringWriter;
 3    public static String getStackTrace(Throwable t)
 4    {
 5        StringWriter sw = new StringWriter();
 6        PrintWriter pw = new PrintWriter(sw, true);
 7        t.printStackTrace(pw);
 8        pw.flush();
 9        sw.flush();
10        return sw.toString();
11    }

12
最近在网上看到一个面试题:
1 Integer a = null;
2 Integer b = a;
3 int c = b;
What will happen?答案当然是NullPointerException。但是为什么?查看以下代码:
     0  aconst_null
     1  astore_1 [a]
     2  aload_1 [a]
     3  astore_2 [b]
     4  aload_2 [b]
     5  invokevirtual java.lang.Integer.intValue() : int [16]
     8  istore_3 [c]
从字节码中我们可以看出,其实对于装箱和拆箱操作,都是编译器在其中做了支持,将int类型转换成Integer类型(调用Integer.valueOf()方法),或将Integer类型转换成int类型(调用Integer.intValue()方法)。
类在什么时候加载为题
 1 class Singleton {
 2     private static final Singleton instance = new Singleton();
 3     
 4     private Singleton() {
 5         System.out.println("Singleton()");
 6     } 
 7     
 8     public static Singleton getInstance() {
 9         return instance;
10     }
11 }
然后当我们有以下一句话:
Singleton singleton = null;
or
Singleton singleton;
此时类会加载吗?直观点,打印"Singleton()"这句话会被执行吗?答案是不会被执行,对第二句话还是好理解的,因为singleton实例根本没有被用到,若要用,首先要初始化所以编译器会最后忽略这句话,所以singleton变量不会出现在字节吗中。对第一句,singleton实例会出现在字节码中,并且会赋null的值,但是此时Singleton类还是没有被加载,那么此时singleton这个实例是什么类型呢?这点我有点想不通。或者Java的引用在内存中根本是没有类型的,保证类型安全是在编译器端做的,所以在给singleton实例赋null值得时候,只是表明singleton是一个指向null的引用而已,它并没有指向Singleton实例,所以此时Singleton类不需要加载,只有到真正使用到Singleton类的时候才会去加载Singleton类,并实例化instance成员。
这样就引出另一个问题:
有人说把初始化放在getInstance()方法中,会使instance在用到时才被加载,而不是刚开始程序初始化时就被加载,在C++中,这个确实是这样的,但是在Java中有必要这么做吗?从上述的分析中,我们可以看到,其实Java中根本没有必要,只要像上面一样写就可以了。

posted on 2011-09-08 01:15 DLevin 阅读(1335) 评论(0)  编辑  收藏 所属分类: Core Java

只有注册用户登录后才能发表评论。


网站导航: