为什么会抛出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
 1历程杂技import java.io.PrintWriter;
 2历程杂技import 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中根本没有必要,只要像上面一样写就可以了。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-22
  • 2021-09-14
  • 2021-12-13
猜你喜欢
  • 2021-09-06
  • 2022-12-23
  • 2021-07-08
  • 2021-04-14
  • 2021-08-06
  • 2021-11-28
相关资源
相似解决方案