阻塞队列与定时器

阻塞队列用数组组织数据,实现的关键是用两把锁实现,即给入队和出队都加上同一把锁,此时读写操作就必须分开进行,读的时候不能写,写的时候不能读,互不影响,防止发生线程不安全,再配合wait和noitfy方法,读操作必须等写操作执行完了唤醒读操作才能继续进行读操作,反之亦然.

public class TestDemo1 {//阻塞队列实现

    static class MyBlockQueue {
        int[] array = new int[10];
        int size;
        int head;
        int last;

        public MyBlockQueue() {
        }

        public void put(int n) {
            synchronized (this) {
                if (size == array.length) {
                    try {
                        System.out.println("等待取数据");

                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                array[last] = n;
                last++;
                if (last == array.length) {
                    last = 0;
                }
                size++;
                notify();
            }
        }

        public int outQueue() {
            synchronized (this) {
                if (size== 0) {
                    try {
                        System.out.println("等待放数据");
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                int temp = array[head];
                head++;
                if (head == array.length) {
                    head = 0;
                }
                size--;
                this.notify();
                return temp;
            }
        }
    }

    static MyBlockQueue m1 = new MyBlockQueue();

    public static void main(String[] args) {
        Thread t1 = new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < 20; i++) {
                    System.out.println("我是线程一,放数据");
                    m1.put(i);
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 20; i++) {
                    System.out.println("我是线程一,取数据");

                    m1.outQueue();
                }
            }
        };
        t1.start();
        t2.start();
    }
}

结果:
在这里插入图片描述

定时器:

public class ThreadDemo5{
    private static PriorityBlockingQueue<MyTask> p1 = new PriorityBlockingQueue<MyTask>();

    static class MyTask implements Comparable<MyTask> {
        long time;
        Runnable command;

        public MyTask(Runnable r1, long time) {
            this.command=r1;
            this.time =System.currentTimeMillis()+time;
        }
        public void run(){
            this.command.run();;
        }
        @Override
        public int compareTo(MyTask o) {
            return (int)(this.time-o.time);
        }
    }
    class MyThread extends Thread{
        @Override
        public void run() {
            while (true) {
                try {
                    MyTask temp=p1.take();
                    if (temp.time > System.currentTimeMillis()) {
                        p1.put(temp);
                    }else{
                        temp.run();
                    }
                    } catch (InterruptedException e) {
                    break;
                }
            }
        }
    }

    static public void schedule(Runnable r1, long time) {
        MyTask m1 = new MyTask(r1, time);
        p1.add(m1);
        synchronized (ThreadDemo5.class) {
            ThreadDemo5.class.notify();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值