zoukankan      html  css  js  c++  java
  • 自己写个一个队列和单例模式

    package com.bjsxt.base.conn009;
    
    import java.util.LinkedList;
    import java.util.concurrent.atomic.AtomicInteger;
    /**
     * 模拟Queue
     * @author alienware
     *
     */
    public class MyQueue {
    
        private final LinkedList<Object> list = new LinkedList<Object>();
        
        private final AtomicInteger count = new AtomicInteger(0);
        
        private final int maxSize;
        private final int minSize = 0;
        
        private final Object lock = new Object();
        
        public MyQueue (int maxSize){
            this.maxSize = maxSize;
        }
    
        public void put (Object obj) {
            synchronized(lock){
                while(count.get() == maxSize){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.add(obj);
                count.getAndIncrement();
                System.out.println(" 元素 " + obj + " 被添加 ");
                lock.notify();
                
            }
        }
        
        public Object take(){
            Object temp = null;
            synchronized (lock) {
                while(count.get() == minSize){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                count.getAndDecrement();
                temp = list.removeFirst();
                System.out.println(" 元素 " + temp + " 被消费 ");
                lock.notify();
            }
            return temp;
        }
        
        public int size(){
            return count.get();
        }
        
        
        public static void main(String[] args) throws Exception {
            
            final MyQueue m = new MyQueue(5);
            m.put("a");
            m.put("b");
            m.put("c");
            m.put("d");
            m.put("e");
            System.out.println("当前元素个数:" + m.size());
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    m.put("h");
                    m.put("i");
                }
            }, "t1");
            
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(1000);
                        Object t1 = m.take();
                        //System.out.println("被取走的元素为:" + t1);
                        Thread.sleep(1000);
                        Object t2 = m.take();
                        //System.out.println("被取走的元素为:" + t2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "t2");
    
            t1.start();
            Thread.sleep(1000);
            t2.start();
            
        }
        
        
        
    }

    单例模式

    package com.bjsxt.base.conn011;
    
    public class DubbleSingleton {
    
        private static DubbleSingleton ds;
        
        public static DubbleSingleton getDs(){
            if(ds == null){
                try {
                    //模拟初始化对象的准备时间...
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (DubbleSingleton.class) {
                    if(ds == null){
                        ds = new DubbleSingleton();
                    }
                }
            }
            return ds;
        }
        
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(DubbleSingleton.getDs().hashCode());
                }
            },"t1");
            Thread t2 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(DubbleSingleton.getDs().hashCode());
                }
            },"t2");
            Thread t3 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(DubbleSingleton.getDs().hashCode());
                }
            },"t3");
            
            t1.start();
            t2.start();
            t3.start();
        }
        
    }




    内部类模式
    package com.bjsxt.base.conn011;
    
    public class InnerSingleton {
        
        private static class Singletion {
            private static Singletion single = new Singletion();
        }
        
        public static Singletion getInstance(){
            return Singletion.single;
        }
        
    }
    
    
    


  • 相关阅读:
    Entity Framework Code First 数据迁移
    Tekla Structures 使用类库概览
    从IT的角度思考BIM(三):敏捷开发
    在 IIS MIME 类型中添加 md 扩展名
    使用 windows 计划任务播放音乐文件
    Win10 IIS以及ASP.NET 4.0配置问题日志
    从IT的角度思考BIM(二):模式与框架
    最小生成树算法总结(Kruskal,Prim)
    最短路径算法总结(floyd,dijkstra,bellman-ford)
    大整数运算模板总结
  • 原文地址:https://www.cnblogs.com/duan2/p/7768910.html
Copyright © 2011-2022 走看看