在使用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中根本沒有必要,隻要像上面一樣寫就可以了。