Showing posts with label 常用知识点. Show all posts
Showing posts with label 常用知识点. Show all posts

Wednesday, August 6, 2014

Design Pattern

Singleton

Singleton ensures that at most one instance of a class exists at any given time.
public class SingletonDemo {
    private static SingletonDemo instance = null;
    //set the constructor private
    private SingletonDemo() {

    }

    public static synchronized SingletonDemo getInstance() {
        if (instance == null) {
            instance = new SingletonDemo();
        }
        return instance;
    }
}

Factory

The implementation is really simple
  • The client needs a product, but instead of creating it directly using the new operator, it asks the factory object for a new product, providing the information about the type of object it needs.
  • The factory instantiates a new concrete product and then returns to the client the newly created product(casted to abstract product class).
  • The client uses the products as abstract products without being aware about their concrete implementation.

The advantage is obvious: New shapes can be added without changing a single line of code in the framework(the client code that uses the shapes from the factory). As it is shown in the next sections, there are certain factory implementations that allow adding new products without even modifying the factory class.
public class ProductFactory{
    public Product createProduct(String ProductID){
        if (id==ID1)
            return new OneProduct();
        if (id==ID2) return
            return new AnotherProduct();
        // so on for the other Ids
  
        return null; //if the id doesn't have any of the expected values
    }
    
}

Tuesday, July 29, 2014

Java多线程入门(二) Synchronized and Lock

转自Tia—Java synchronized 和 lock
一个process里面所有的threads是共享同一块memory space的, 所以有时候就需要去避免两个threads同时修改一块resource所引起的问题。 Java里引入Synchronized 和 Lock的概念。

1), synchronized method
?
1
2
3
4
5
class Myobject{
  public synchronized void foo(String name){
   .......................................
  }
}
synchronized 关键字限制的是Myobject这个类的一个对象的多个threads不能访问同一个资源,不同对象的线程是可以同时访问的。

2), 一个class多个synchronized 方法:
?
1
2
3
4
5
6
7
8
class Myobject{
  public synchronized void bar(String name){
   。。。。。。。。。
  }
  public synchronized void foo(String name){
   。。。。。。。。。
  }
}
Java的每个对象都有一个锁(lock或monitor), 当一个thread访问synchronized方法时, 该对象就被锁住了, 在解锁(执行完毕或抛出异常)之前, 该对象的任何thread都不能再访问其他的synchronized的方法。注意:是对象被上锁,而不是一个synchronized方法一个锁。

3), Synchronized 和 Static Synchronized
?
1
2
3
4
5
class Myobject{
   public static synchronized void bar(String name){
    。。。。。。。。。
   }
}
静态变量时属于class的,不是属于对象的, 若一个thread访问static synchronized 方法时, 则这个class被上锁,在解锁之前,这个class的所有对象的所有thread都不能访问该方法和其他synchronized的方法。

4), Synchronized Block
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Myobject{
  public void foo(String name){
    synchronized(this){
     。。。。。。。。。
    }
  }
}
class Myobject{
  private AnotherClass object = new AnotherClass();
  public void foo(String name){
    synchronized(object){
     。。。。。。。。。
    }
  }
}
Block用来同步一段代码, 和synchronized method一样,对于每个对象,它只有一个thread可以访问Synchronized block里面的东西。注意synchronized里面的object可以是任意类的object。

5), Locks
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Myobject {
  private Lock lock;
  public Myobject(){
    lock = new ReentrantLock();
  }
  public void foo(){
    lock.lock();
      ........
    lock.unlock();
 }
}

和synchronized类似,一个对象在某一个时间只能有一个thread holds the lock。 避免了lock之间的资源被其他thread随意修改。 比较典型的例子是ATM取款存款机。
同时我们也可以用Lock生成各种condition,方便我们进行操作