三种设计模式的实现

1.消费者生产者模式

 1 import java.util.PriorityQueue;
 2 import java.util.Queue;
 3 
 4 public class ProducerAndConsumer {
 5     private final int capacity = 100;
 6     Queue<Integer> que = new PriorityQueue<>(capacity);
 7     public static void main(String[] args) {
 8         ProducerAndConsumer p = new ProducerAndConsumer();
 9         Thread t1 = new Thread(p.new Producer());
10         Thread t2 = new Thread(p.new Consumer());
11         t1.start();
12         t2.start();
13     }
14     class Producer implements Runnable{        
15         @Override
16         public void run() {
17             // TODO Auto-generated method stub
18             while (true) {
19                 synchronized (que) {
20                     while (que.size() == capacity) {
21                         try {
22                             wait();
23                         } catch (InterruptedException e) {
24                             // TODO Auto-generated catch block
25                             e.printStackTrace();
26                             notify();
27                         }
28                     }
29                     que.offer(1);
30                     notify();
31                 }                
32             }            
33         }
34     }
35     class Consumer implements Runnable{
36         public void run() {
37             while (true) {
38                 synchronized (que) {
39                     while (que.size() == 0) {
40                         try {
41                             wait();
42                         } catch (InterruptedException e) {
43                             // TODO Auto-generated catch block
44                             e.printStackTrace();
45                             notify();
46                         }
47                     }
48                     que.poll();
49                     notify();
50                 }                
51             }            
52         }        
53     }    
54 }
View Code

2.单例模式

需要注意构造方法需要设置为private,防止类在类外被实例化,获得类实例的唯一方法是通过getInstance();然后就是多线程加锁的处理。

 1 package 单例模式;
 2 
 3 /*饿汉式*/
 4 /*
 5 public class Singleton {
 6     private static final Singleton sg = new Singleton();
 7     private Singleton() {
 8         
 9     }
10     public static Singleton getInstance() {
11         return sg;
12     }
13 }
14 */
15 /*懒汉式*/
16 //线程不安全,多线程会出现多个实例
17 /*
18 public class Singleton {
19     private static Singleton sg;
20     private Singleton() {
21         
22     }
23     public static Singleton getInstance() {
24         if (sg == null) {
25             return new Singleton();
26         }        
27         return sg;
28     }
29 }
30 */
31 //多线程懒汉模式
32 public class Singleton {
33     private volatile static Singleton sg;
34     private Singleton() {
35         
36     }
37     public static Singleton getInstance() {
38         if (sg == null) {
39             synchronized (sg.getClass()) {
40                 if (sg == null) {//不加这一行还是会造成线程不安全
41                     return new Singleton();
42                 }
43             }            
44         }        
45         return sg;
46     }
47 }
View Code

 

3.工厂模式

http://blog.csdn.net/jason0539/article/details/23020989

http://blog.csdn.net/jason0539/article/details/44976775

posted @ 2017-04-10 15:38  fisherinbox  阅读(158)  评论(0编辑  收藏  举报