顯示具有 JavaMemo 標籤的文章。 顯示所有文章
顯示具有 JavaMemo 標籤的文章。 顯示所有文章

2013年10月22日 星期二

[Java筆記] Object Oriented

  • 使用 Annotation "@Override" 標示是個 overriding method,
    若 superclass 不包含同 signature 的 method, 會出現compiler error.
  • Defining a Method with the Same Signature as a Superclass's Method
    Superclass Instance Method Superclass Static Method
    Subclass Instance Method Overrides Generates a compile-time error
    Subclass Static Method Generates a compile-time error Hides
  • Overriding:
    不允許 throw new checked exception,
    但 unchecked exception 可以 (e.g. RuntimeException).
  • Covariant return type:
    the return type of the overriding method is
    a subtype of the return type of the overridden method.
    • e.g.
      class AA
      class A extends AA
      class B extends A
      class C extends B

      class superTest{
          public A method1(){
              return new A();
          }
      }

      class subTest extends superTest{
          public B method1(){ // or public C method1()
              return new C();
          }
      }

  • Method inner class
    • e.g.
      void method1(){
          class A {
              // some implement.
          }
      }
  •  Singleton Pattern
    • Constructor 設為 private
    • Method getInstance() 設為 static 及 synchornized
    • Override method clone()
  • Cohesion: the whole of a class sticks together.
    Coupling: the inter dependency between 2 or more classes.
  • Ideally design: high cohesion and loose coupling.


---

2013年10月7日 星期一

[Java筆記]Concurrency

  • java.util.concurrent.atomic.*
    All classes have get and set methods that work like reads and writes on volatile variables.
 
  • ReentrantLock 在進入 wait state 之前, 會暫時 release the lock.
  • ReentrantLock有2個contructor
    public ReentrantLock()
    public ReentrantLock(boolean fair): the longest waiting thread will get the lock next.
  • ReentrantLock 可視為一種取代傳統的 "wait-notify" 機制.

  • Executor interface:
    傾向使用已存在的 worker threads.
  • Thread pools:
    減少 thread creation 的 overhead.


  • Fork/join framework:
    An implementation of the ExecutorService interface.
    利於使用在 multiple processors 的環境.


  • interface ConcurrentHashMap<K, V>
    extends AbstractMap<K, V>
    implements ConcurrentMap<K, V>, Serializable
  • interface ConcurrentMap<K, V>
    extends Map<K, V>

2013年9月30日 星期一

[Java筆記] Threads

  • Thread有5個狀態
    • New
    • Runnable
    • Running
    • Waiting/Sleeping/Blocked
    • Dead
  • Modifier synchronized 不能用在constructor/variables上.
  • static synchronized method's lock is on the class-instance.
    non-static synchronized method's lock is on the object instance.
  • Thread deadlocking is the situation when thread execution grinds to a halt   
    because the code is waiting for locks to be removed from objects.      
    It occurs when both threads are waiting for each others' locks to be released.      
  • Method "long[] findDeadlockedThreads()" 用來查詢deadlock threads.   
    回傳thread IDs.   
    用於monitors或ownable synchronizers(e.g. ReentrantLock/ReentrantReadWriteLock).
    http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html#findDeadlockedThreads()
  • Memory consistency:理解 the happens-before relationship 可幫助避免 memory consistency error.    
    Happens-before relationship: 
    a guarantee that memory writes by one specific statement are visible to another specific statement.







    複習:
    • Constructors are never inherited.