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>