<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    上善若水
    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不要釋放鎖,所以也就不拋出異常。

      除去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 閱讀(1353) 評論(0)  編輯  收藏 所屬分類: Core Java
    主站蜘蛛池模板: 亚洲中文字幕无码久久2017| 亚洲五月午夜免费在线视频| 亚洲成a人片在线观看中文动漫 | 亚洲av无码久久忘忧草| 暖暖日本免费中文字幕| 国产亚洲日韩在线三区| 香蕉免费在线视频| 亚洲不卡中文字幕无码| 日本免费污片中国特一级| 久久久久亚洲av无码专区喷水| 在线毛片片免费观看| 亚洲av无码不卡一区二区三区| 久久99热精品免费观看动漫| 亚洲精品网站在线观看你懂的| 最近2022中文字幕免费视频| 亚洲一卡二卡三卡四卡无卡麻豆| 中文字幕无码免费久久99| 亚洲国产精品网站在线播放 | 国产亚洲精品第一综合| 亚洲国产精品一区二区第四页| 九九免费精品视频在这里| 亚洲日产无码中文字幕| 最近免费2019中文字幕大全| 亚洲人成在线播放| 国产精品四虎在线观看免费 | **一级毛片免费完整视| 亚洲三级在线播放| 免费jjzz在在线播放国产| a毛片在线还看免费网站| 久久久久亚洲av无码专区导航| 成人毛片手机版免费看| 深夜福利在线视频免费| 亚洲国产精品人久久| 成年女人男人免费视频播放| 免费人成视频在线观看免费| 亚洲AV日韩精品久久久久久| 免费的一级黄色片| 国产成人免费ā片在线观看老同学| 亚洲欧洲日本国产| 亚洲精品色婷婷在线影院| 99视频在线免费看|