Java 类集框架: Stack 栈 与 Queue 队列

栈 Stack

简介

栈是一种先进后出的数据结构. 比如是浏览器上的后退, 以及各个编辑器上的撤销操作都属于栈的功能, 而在整个的类集里面, 提供有Stack, 这个类是 Vector 的子类. 但是需要注意的是使用这个子类的时候, 可不是使用 Vector 类的方法, 同样, 使用这个类的时候, 不要使用向上转型.
因为要操作的方法不是List定义的, 而是由 Stack 定义的.

栈的核心方法:

  • 入栈:public E push(E item)
  • 出栈:public E pop()
  • 查看栈顶元素: public E peek()

范例: 观察出入栈

package com.beyond.nothing;

import java.util.Stack;

public class test {
    
    
    public static void main(String[] args) {
    
    
        Stack<String> all = new Stack<>();
        all.push("A");
        all.push("B");
        all.push("C");
        while (!all.empty()) {
    
    
            System.out.println(all.pop());
        }
    }
}

Stack 主要用于理论上, 而在实际开发之中使用的机率不高. 是为之后学习 JVM 准备的.

队列 Queue

简介:

Stack 是一种先进后出, 但是还有一个与之类型是为先进先出, 称之为队列.
在java.util 包中使用 Queue 来实现对队列的处理操作

在这里插入图片描述

Queue 接口主要是进行先进先出的时候, 在这个接口里面有如下的方法:

  • 依照队列取出内容: public E poll()

范例: 使用 Queue

package com.beyond.nothing;

import java.util.LinkedList;
import java.util.Queue;

public class test {
    
    
    public static void main(String[] args) {
    
    
        Queue<String> queue = new LinkedList<>();

        queue.add("A");
        queue.add("B");
        queue.add("C");
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }
}

在这里插入图片描述

既然队列在整个的操作之中可以起到一个缓存作用, 那么就可以利用队列去修改之前多线程所涉及的生产者和消费者程序模型.

范例: 利用队列实现生产者与消费者

package com.beyond.nothing;

import java.util.LinkedList;
import java.util.Queue;

public class test {
    
    
    public static void main(String[] args) {
    
    
        DataQueue data = new DataQueue();
        new Thread(new DataProvider(data)).start();
        new Thread(new DataConsumer(data)).start();
    }
}

class DataProvider implements Runnable {
    
    

    private DataQueue data;

    public DataProvider(DataQueue data) {
    
    
        this.data = data;
    }

    @Override
    public void run() {
    
    
        for (int x = 0; x < 50; x++) {
    
    
            if (x % 2 == 0) {
    
    
                this.data.product("DHL", "是个好人");
                try {
    
    
                    Thread.sleep(50);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            } else {
    
    
                this.data.product("XXX", "笨蛋");
                try {
    
    
                    Thread.sleep(50);
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }
            }

        }
    }

}

class DataConsumer implements Runnable {
    
    
    private DataQueue data;

    public DataConsumer(DataQueue data) {
    
    
        this.data = data;
    }

    @Override
    public void run() {
    
    
        for (int x = 0; x < 50; x++) {
    
    
            try {
    
    
                Thread.sleep(150);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            this.data.consumer();
        }
    }
}


class DataQueue {
    
     // 数据的生产队列

    private class Data {
    
      // 负责数据保存
        private String title;
        private String note;
        //flag = true : 表示允许生产, 但是不允许消费者取数据
        //flag = false : 表示不允许生产, 但是消费者可以进行取数据

        public synchronized void set(String title, String note) {
    
    
            this.note = note;
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            this.title = title;
        }

        public synchronized Data get() {
    
    
            try {
    
    
                Thread.sleep(1000);
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
            return this;
        }

        @Override
        public String toString() {
    
    
            return "Data{" +
                    "title='" + title + '\'' +
                    ", note='" + note + '\'' +
                    '}';
        }
    }


    private boolean flag = false;
    Queue<Data> queue = new LinkedList<>();

    // 生产数据
    public synchronized void product(String title, String node) {
    
    
        Data data = new Data();
        data.set(title, node);
        this.queue.add(data);
        super.notify();
    }

    // 取走数据
    public synchronized void consumer() {
    
    
        Data data = this.queue.poll();
        if (data == null){
    
    
            try {
    
    
                super.wait();  // 进行等待
            } catch (InterruptedException e) {
    
    
                e.printStackTrace();
            }
        }
        System.out.println(data);
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Beyond_Nothing/article/details/113342368