阻塞队列与定时器

阻塞队列用数组组织数据,实现的关键是用两把锁实现,即给入队和出队都加上同一把锁,此时读写操作就必须分开进行,读的时候不能写,写的时候不能读,互不影响,防止发生线程不安全,再配合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();
    }
}

结果:
阻塞队列与定时器_第1张图片

定时器:

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();
        }
    }

你可能感兴趣的:(java,java)