ex. Normal Distribution Calculator (I can't launch it on Chrome browser, but it works well on IE7)

C++ | Java | |
Pure Virtual Function | vitual void func() = 0; | abstarct void func(); |
Impure Virtual Function | vitual void func(); | void func(); |
Non-Virtual Function | void func(); | final void func(); |
Hiding Inherited Names | Yes | No |
Prohibiting Inheritance | No | final class |
上學期陳恭教授的高等軟體設計,有請博班的學長介紹Java Swing的Event-Driven Architecture,
可惜當時不夠專心,沒聽的很懂。有機會一定要再跟博班的學長copy一份slide.
下面是幾個重要的reference:
Thread and Swing (官方文件)
Using a Swing Worker Thread (官方文件)
Towards a Timely, Well-Balanced, Event-Driven Architecture
Swing threading and the event-dispatch thread
InputMap inputMap = textArea.getInputMap();
ActionMap actionMap = textArea.getActionMap();
Object transferTextActionKey = "TRANSFER_TEXT";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0),transferTextActionKey);
actionMap.put(transferTextActionKey,new AbstractAction() {
public void actionPeformed(ActionEvent e) {
textField2.setText(textField.getText());
textField.setText("");
textField2.requestFocus();
}
});
我記得以前在windows下跑eclipse3.2,
若要使eclipse隨滑鼠游標自動顯示document help,如下圖.
只要
Window> Preferences
Java> Installed JREs
Edit>JRE home directory
設JDK路徑(而非JRE路徑唷!)便可~
但在ubuntu下,我一直找不到j2sdk這個目錄,但是我確實有安裝sun-java6-jdk這個套件.
Eclipse的
Window> Preferrence>
Java> Build Path> Classpath Variables 的變數 JRC_SRC(reserved) -empty
並不允許user去改它!!
結果我在Synaptic Package Manager上找到sun-java6-source,並安裝它, 重開eclipse.
JRC_SRC 變數就會自動被改成/usr/lib/jvm/java-6-1.6.0.03/src.zip
然後eclipse的document help就啟動了~~
進入waiting queue的兩種可能:
1. 被lock (synchronized block) 擋在critical section外面.
2. call wait() method.
離開waiting queure的可能:
1.call notify() method.
2.call notifyAll() method.
3.call interrupt() method.
4.time out occur. (透過call wait( long time ) 來實現)
yield() v.s. sleep() v.s. wait() v.s. join()
1. yield() thread 沒有離開monitor,只是暫時將執行權交給其它的thread (content switch ?)
2. sleep(1000),thread 沒有離開monitor, 只是暫停執行1000ms.
3. wait(1000) ,thread離開monitor, 移到waiting queue, 若1000ms後仍未被喚醒, 便離開waiting queue, 重新競爭lock;若無設time out,即wait(),則移至waiting queue等待。直到notify(), notifyAll(), interrupt() 被呼叫時,才會便離開waiting queue, 重新競爭lock.
4. join(),直到此thread執行完,才將執行權讓出。也可設time out。
interrupt() 把沈睡的thread半途打醒.
1. 用來中斷wait(), sleep(), join()等狀態的thread.
2. 會從wait(), sleep(), join() 中丟出InterruptedException, thread的控制權便會轉入catch block.
3. thread wati()時,會離開monitor,移到waiting queue,所以當wait狀態的thread被呼叫interrupt()時,需先重新搶到lock,才會丟出interruptedException. 取得lock前都不會丟出interruptedException.
4. wait(), sleep(), join()都會不斷檢查interrupt status,而interrupt()便是把interrupt status設為true,當wait(), sleep(), join()發現interrupt status為true時,便丟出interruptedException.
5. isInterrupted() 可以回傳interrupt status為何.
6. interrupted() 除了回傳interrupt status外,並將interrupt status設為false.
interrupt() v.s. notify() and notifyAll()
1.notify()及notifyAll()是喚醒在waiting queue的thread.
2.interrupt()是直接中斷sleep() , wait(), join()狀態的thread.
1.Effective java (有中譯本)
2.Practical java (有中譯本)
3.Thinking in Java (有中譯本)
4.Design Pattern (這本是指四人幫寫的,有中譯本。很難懂!失眠時的良藥..XD)
5.Head First Design Pattern (有中譯本)
6.Java pitfalls (有中譯本)
7.More java pitfalls (有中譯本)
8.Java深度歷險 (已絕版,有錢也買不到...Orz)
9.Java Multithread Design Pattern ("結城 浩"所著,這本有日翻中的中譯本)
10.Design Patterns於Java語言上的實務應用 ("結城 浩"所著,這本有日翻中的中譯本)
今天總算釐清了一些困擾我很久的東西,所以做個簡單的筆記。
一、安裝單一版本的jdk時,通常會有:
4個java.exe:
1.WINDOWS\system32\java.exe
2.[JRE_location]\bin\java.exe
3.[JDK_location]\bin\java.exe
4.[JDK_location]\jre\bin\java.exe
2個JRE:
1.Public JRE (指[JRE_lcation])
2.Private JRE (指[JDK_location]\jre)
前兩個java.exe使用的是Public JRE,後兩個java.exe使用的是Private JRE。
二、classpath無需設定rt.jar的路徑,因為jdk會預設載入。(參考六)
可以在console mode 輸入java -verbose得之。
(verbose 可以顯示JVM會依序load哪些Class)
不知道是幾版之後才如此。總之,我試了又試,有沒有設rt.jar路徑真的無差別,
很好奇為什麼大家都說要在classpath設定rt.jar,而我在google上卻找不到答案。
也有人提倡最好少設classpath。
三、path用來設定指令的前置位址,如下:
(可以在console mode 輸入set path 或 path以顯示)
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;
C:\Program Files\Microsoft SQL Server\90\Tools\binn\;
C:\Program Files\Java\jdk1.6.0\bin;
C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\Bin;
所以當我們在console mode 輸入java XXX 時,如果current目錄沒有java這指令時,
便會依序嘗試path下的路徑。
四、jdk目錄下之所以安置了一個jre,原因在於jdk下的工具大都是用java開發的。
如:java.exe , javac.exe jar.exe..等,(這些工具都放在jdk\lib\tools.jar中)
外表雖然是用RPC包裝成.exe檔,但其實它們都需要呼叫JRE的library。
所以JDK下就放了一個專供JDK使用的JRE,
但也可以透過path的設定讓[JDK_location]\jre被一般使用者使用。
五、在console mode使用java 這指令時,它使用的是哪個jre呢?
1.先找自己的目錄下是否有jre.
2.父目錄底下的jre目錄.(jdk\bin下的工具便是如此找到jdk\jre)
3.查詢Windows Registry
(HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\)
所以這時path的順序設定將決定採用的是哪一個jre。
case1:
若path的順序是\WINDOWS\system32 在 \[JDK_location]\bin 之前。
java指令便是執行\WINDOWS\system32\java.exe
由於windows下無jre目錄,所以會參考Window Registry下的jre。
(即前述的Public JRE)
case2:
若path 的順序是\[JDK_location]\bin 在\windows\system32 之前。
那麼java指令便是執行C:\[JDK_location]\bin\java.exe
由於jdk下有一個jre目錄,所以java會參考jdk\jre。
(即前述的Private JRE)
最常發生的問題是當path的順序如case1,
java指令找到的jre與javac找到的jre是不同的。
產生的問題待第六點解釋。
六、java找尋class的順序
根據JDK 文件說明, java以下面3種順序找尋class的順序
1.Bootstrap classes
2.Extension classes
3.Users classes
Bootstrap classes指的是java在啟動時載入的class,
這些class主要是rt.jar 和 jre/lib 目錄下的一些class。
(所以rt.jar可以不須加入classpath)
下面列出系統中預設的Bootstrap classes:
jre\lib\rt.jar; jre\lib\i18n.jar;
jre\lib\sunrsasign.jar; jre\lib\jsse.jar;
jre\lib\jce.jar; jre\lib\charsets.jar;
jre\classes
Extension classes 指的是jre/lib/ext 目錄下的jar或zip檔。
third-party 的 library通常會放在這目錄下,如java3d, jogl..等。
當classloader 需要resolve class時,jre會自動來此目錄下找。
如果不同名字的jar,卻包含了相同的class,那麼哪一個class被載入是不一定的。
第五點最後提到的問題,常常跟Extension classes扯上關係。
當我們使用third-party的library時,如果只將jar檔放在Private JRE/lib/ext中,
javac指令因為使用Private JRE,所以能夠找到third-party library,得以順利compile。
但java指令使用的是Public JRE,Public JRE找不到 third-party library,
以致無法順利執行,原因是無法import package。
解決的方法有:
1.將third-party library jar檔 在 private JRE/public JRE 各放一份。
2.設定path時,使用第五點的case2。使java及javac指都優先使用Private JRE。
Eclipse可以很方便使用third -party library,
Project->Properties->Java Build Path->Libraries->Add External JARs
(陶百學長教我的)
User classes便是指我們在classpath設定路徑下的class。
如:若總是使用c:\java\的class,或是需要使用c:\lib\test.jar裡的library,
便可以set classpath =.;c:\java\;c:\lib\test.jar
(.jar檔須將完整路徑名加入classpath, jre才會進入該jar檔裡找尋target class)
user classes尋找的優先序:
(1)若無設定classpath, jre預設找(.)目前目錄是否存在target class.
(2)若有設定classpath, jre只找classpath設定的路徑是否存在target class.
(3)在console mode下使用-cp 或 -classpath, 則jre只找參數後的路徑是否存在target class.
(4)在console mode使用-jar來指定jar檔時, jre只找尋jar中是否存在target class.
reference:
classpath詳解
http://www.phpchina.com/755/viewspace_5055.html
JavaWorld
classPath的討論串
path與classpath