天天看点

历程杂技

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

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

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

  除去properites文件路径错误、拼写错误外,出现"could not resolve placeholder"很有可能是使用了多个propertyplaceholderconfigurer或者多个<context:property-placeholder>的原因。

在spring 3.0中,可以写: 

<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" />与下面的配置是等价的

<bean id="随便" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer">  

    <property name="location" value="xxx.properties" />  

    <property name="ignoreunresolvableplaceholders" value="true" />   

</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();

历程杂技

}

历程杂技

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中根本没有必要,只要像上面一样写就可以了。