2012年12月21日 星期五

User Thread and Daemon Thread

Java 的 Thread 分兩種:
User Thread(Defalut) 和 Daemon Thread.

User Thread 被停止時,JVM 會看是否還有其他 User Thread 在執行,如果有其他 User Thread 在執行,則該 Thread 不會被停止。

Daemon Thread 是一個背景執行緒,並且會跟著創造他的 Thread (father Thread) 的生命週期。

程式裡的差別就是 Thread 內的 setDaemon(boolean) 方法。(此 method 要在 Thread.start() 之前就要決定了)

以下的程式碼可以試著把 setDaemon 打開測試。 也可以把 interrupt 打開測試。

public class TryThread extends Thread{
 private String lastname;
 private long delay;
 
 public TryThread(String lastname, long delay) {
  
  this.lastname = lastname;
  this.delay = delay;
  //this.setDaemon(true);
 }
 
 public static void main(String[] args){
  Thread t1 = new TryThread("cristo", 2000L);
  Thread t2 = new TryThread("eric", 3000L);
  Thread t3 = new TryThread("john", 5000L);
  System.out.println("Press Enter...");
  t1.start();
  t2.start();
  //t3.setDaemon(false);
  t3.start();

  try{  
   System.in.read();
   System.out.println("Enter pressed...");
   //t1.interrupt();
   //t2.interrupt();
   //t3.interrupt();
  }catch(Exception e){
   System.out.println(e);
  }
  System.out.println("Enter main()");
  return;
 }

 @Override
 public void run() {
  try{
   while(true){
    System.out.println(lastname);
    sleep(delay);
   }
  }catch(Exception e){
   System.out.println(e);
  }
 }
}
以上參考 Ivor.Hortons.Beginning.Java.Java.7.Edition. CH16 Understanding Threads

2012年12月15日 星期六

Java Developer Most Useful Books

Source : http://www.intermediatejava.com/2011/06/the-books-most-useful-for-a-java-developer/

  • Joshua Bloch, Effective Java (2nd Edition).
    經典中的經典,此書很推薦初學者看
  • Christian Bauer and Gavin King, Java Persistence with Hibernate.
  • Brian Goetz with Tim Peierls, Joshua Bloch, Joseph Bowbeer, David Holmes, and Doug Lea, Java Concurrency in Practice
  • Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software
  • Steve McConnell, Code Complete: A Practical Handbook of Software Construction
  • Martin Fowler with Kent Beck, John Brant, William Opdyke, and Don Roberts, Refactoring: Improving the Design of Existing Code
  • Maurice Naftalin and Philip Wadler, Java Generics and Collections
  • David Geary and Cay S. Horstmann, Core JavaServer Faces (3rd Edition)
  • Eben Hewitt, Java SOA Cookbook
  • Chet Haase and Romain Guy, Filthy Rich Clients: Developing Animated and Graphical Effects for Desktop Java Applications
  • Debu Panda, Reza Rahman, and Derek Lane, EJB 3 in Action
  • Ira R. Forman and Nate Forman, Java Reflection in Action
  • John Zukowski, The Definitive Guide to Java Swing (3rd Edition)
  • Scott Oaks and Henry Wong, Java Threads (3rd Edition)
  • Robert Sedgewick and Kevin Wayne, Algorithms (4th Edition)
  • Christopher Steel, Ramesh Nagappan, and Ray Lai, Core Security Patterns: Best Practices and Strategies for J2EE™, Web Services, and Identity Management
  • Steven Haines, Pro Java EE 5 Performance Management and Optimization
  • Cay S. Horstmann and Gary Cornell, Core Java, Volume 1– Fundamentals (8th Edition)
  • Cay S. Horstmann and Gary Cornell, Core Java, Volume 2– Advanced Features (8th Edition)
  • James Gosling, Bill Joy, Guy Steele, and Gilad Bracha, The Java Language Specification
  • 2012年2月26日 星期日

    install mysql 5.5 on mac (dmg)

    1. download dmg file

    http://dev.mysql.com/downloads/mysql/
    direct download link from website

    2. install file

    .pkg -- two files
    .prefPane -- one file (for system preferences..)

    3. start mysql




    4. change password

    open terminal
    > cd /usr/local/mysql/bin
    > ./mysqladmin -h localhost -u root password [your password]

    5. login
    > ./mysql -u root -p

    6. start mysql in command line

    > cd /usr/local/mysql/support-files
    > sudo ./mysql.server start

    2012年1月30日 星期一

    mac maven tools.jar dependency

      <profiles>
    <profile>
    <id>windows</id>
    <activation>
    <os>
    <family>Windows</family>
    </os>
    </activation>
    <properties>
    <toolsjar>${java.home}/../lib/tools.jar</toolsjar>
    </properties>
    </profile>
    <profile>
    <id>osx</id>
    <activation>
    <os>
    <family>mac</family>
    </os>
    </activation>
    <properties>
    <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
    </properties>
    </profile>
    </profiles>

    2011年12月11日 星期日

    Unable to set the activation state to true for the application .....weblogic.common.ResourceException: is already bound

    weblogic 在 bound DataSource 時,一直出錯。

    檢查了設定檔、Connection 都很正常。

    經過 google 後才發現,原來是 weblogic 設定檔的問題。

    從 domains\xxx\config\jdbc\xxxxxx-jdbc.xml
    在 除了設定好的部份,還有空白值??

    把空白多出來的 tag 刪除就正常了。

    2011年11月30日 星期三

    org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken

    在 weblogic 上使用 hibernate 時,幾乎都會發生此錯誤。
    原因為 hibernate 在 parse Name Query 會出問題。
    目前已知是 antlr 的問題,hibernate 需要 antlr.jar(2.7.6 or 2.7.7) 但是會跟 weblogic.jar 衝突。而通常 classpath 會先 load 到 weblogic server 內的 jar files. 所以hibernate 會認錯 class file。




    方法一
    修改 domain 下的 startWeblogic.bin (startWeblogic.sh) , 讓 weblogic 優先載入 antlr 。
    由於此修改是針對所有的 container , 所以需要評估可能的影響。

    在 ${SAVE_CLASSPATH} 修正載入順序。



    方法二
    修改 WEB-INF/weblogic.xml加入

    <weblogic-web-app>
    ....
    <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
    </container-descriptor>
    ....
    </weblogic-web-app>

    讓 web project 內的 jar 優先使用。



    方法三

    在 Hibernate 相關檔案加入以下設定

    <property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>

    此方法我還沒試成功過...

    2011年11月21日 星期一

    [2011 HK] Day3

    Day3

    飯店晨泳 --> 飯店自助餐 --> 龍城 , k11 --> 機場 check in --> 昂坪纜車 --> 東薈城(citygate outlet)--> 溫暖的家。



    今天早上天氣變好了,還出了太陽。當然還是要好好利用飯店設施,於是又跑去泡了一下。
    今日的早餐為飯店 buffet , 在地下一樓,港畔餐廳(Harbourside),臨星光大道旁。吃的內容很豐富,有中式、日式、西式...
    看著麗星郵輪緩緩駛進維港,配上和煦的陽光、豐富的早餐... 果然很享受。(此餐兩人要 HK 650 左右,還好飯店招待...)

    中午過後先到機場 early check in , 早報到的好處就是可以選機位,而且可以預掛行李。

    接著搭乘 S1 bus 到昂坪, 到的時候沒太多人,可能已經是下午了吧?? 大部分車廂都只做 2 ~ 4 人。
    由於是晚上八點的飛機,所以也沒逛太久,就下山了。


    在 outlet 找不到想買的東西中,結束香港行...