在使用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();
}

參考:
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中根本沒有必要,只要像上面一樣寫就可以了。
posted on 2011-09-08 01:15
DLevin 閱讀(1353)
評論(0) 編輯 收藏 所屬分類:
Core Java