package com.yuzhenc.collection; import java.util.Stack; /** * @author: yuzhenc * @date: 2022-03-20 15:41:36 * @desc: com.yuzhenc.collection * @version: 1.0 */ public class Test26 { public static void main(String[] args) { Stack<String> stack = new Stack<>(); stack.add("A"); stack.add("B"); stack.add("C"); stack.add("D"); System.out.println(stack);//[A, B, C, D] //判断栈是否为空 System.out.println(stack.empty());//false //查看栈顶元素,不会移除 System.out.println(stack.peek());//D System.out.println(stack);//[A, B, C, D] //查看栈顶元素,并且移除,即出栈(先进后出) System.out.println(stack.pop());//D System.out.println(stack);//[A, B, C] //入栈,和add方法执行的功能一样,就是返回值不同 System.out.println(stack.push("E"));//返回入栈的元素 E System.out.println(stack);//[A, B, C, E] } }
ArrayBlockingQueue
: Tidak disokong Operasi membaca dan menulis serentak, lapisan bawah adalah berdasarkan tatasusunan; senarai terpaut;
package com.yuzhenc.collection; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; /** * @author: yuzhenc * @date: 2022-03-20 16:00:22 * @desc: com.yuzhenc.collection * @version: 1.0 */ public class Test27 { public static void main(String[] args) throws InterruptedException { ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3); //添加元素 //不可以添加null,报空指针异常 //arrayBlockingQueue.add(null); //arrayBlockingQueue.offer(null); //arrayBlockingQueue.put(null); //正常添加元素 System.out.println(arrayBlockingQueue.add("Lili"));//true System.out.println(arrayBlockingQueue.offer("Amy"));//true arrayBlockingQueue.put("Nana");//无返回值 //队列满的情况下添加元素 //arrayBlockingQueue.add("Sam");//报非法的状态异常 //设置最大注阻塞时间,如果时间到了队列还是满的,就不再阻塞了 arrayBlockingQueue.offer("Daming", 3,TimeUnit.SECONDS); System.out.println(arrayBlockingQueue);//[Lili, Amy, Nana] //真正阻塞的方法,如果队列一直是满的,就一直阻塞 //arrayBlockingQueue.put("Lingling");//运行到这永远走不下去了,阻塞了 //获取元素 //获取队首元素不移除 System.out.println(arrayBlockingQueue.peek());//Lili //出队,获取队首元素并且移除 System.out.println(arrayBlockingQueue.poll());//Lili System.out.println(arrayBlockingQueue);//[Amy, Nana] //获取队首元素,并且移除 System.out.println(arrayBlockingQueue.take());//Amy System.out.println(arrayBlockingQueue);//[Nana] //清空元素 arrayBlockingQueue.clear(); System.out.println(arrayBlockingQueue);//[] System.out.println(arrayBlockingQueue.peek()); System.out.println(arrayBlockingQueue.poll()); //设置阻塞事件,如果队列为空,返回null,时间到了以后就不阻塞了 System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS)); //真正的阻塞,队列为空 //System.out.println(arrayBlockingQueue.take());//执行到这里走不下去了 } }
LinkedBlockingQueue
: Memindahkan data antara urutan dengan mudah dan cekap tanpa menyebabkan perbalahan data dalam baris gilir; >: Dengan baris gilir Penyekatan keutamaan;
Baris tanpa had, tiada had panjang, tetapi apabila anda tidak menentukan panjang, panjang awal lalai ialah 11, anda juga boleh menentukannya secara manual , sudah tentu apabila data diteruskan Apabila ditambah, lapisan bawah (lapisan bawah ialah Objek tatasusunan[]) secara automatik akan berkembang sehingga semua memori digunakan, menyebabkan limpahan memori OutOfMemoryError Program tidak akan tamat; 🎜> SynchronousQueue
package com.yuzhenc.collection; import java.util.concurrent.SynchronousQueue; /** * @author: yuzhenc * @date: 2022-03-20 21:06:47 * @desc: com.yuzhenc.collection * @version: 1.0 */ public class Test28 { public static void main(String[] args) { SynchronousQueue sq = new SynchronousQueue(); //创建一个线程,取数据: new Thread(new Runnable() { @Override public void run() { while(true){ try { System.out.println(sq.take()); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); //搞一个线程,往里面放数据: new Thread(new Runnable() { @Override public void run() { try { sq.put("aaa"); sq.put("bbb"); sq.put("ccc"); sq.put("ddd"); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } }
PriorityBlockingQueue
ClassCastException
tidak boleh Elemen nol diletakkan dalam baris gilir ini; urusan pesanan: jika tiga puluh selepas membuat pesanan Pesanan akan dibatalkan secara automatik jika tiada pembayaran dalam masa beberapa minit
package com.yuzhenc.collection; import java.util.concurrent.PriorityBlockingQueue; /** * @author: yuzhenc * @date: 2022-03-20 21:16:56 * @desc: com.yuzhenc.collection * @version: 1.0 */ public class Test29 { public static void main(String[] args) throws InterruptedException { PriorityBlockingQueue<Human> priorityBlockingQueue = new PriorityBlockingQueue<>(); priorityBlockingQueue.put(new Human("Lili",25)); priorityBlockingQueue.put(new Human("Nana",18)); priorityBlockingQueue.put(new Human("Amy",38)); priorityBlockingQueue.put(new Human("Sum",9)); //没有按优先级排列 System.out.println(priorityBlockingQueue);//[Human{name='Sum', age=9}, Human{name='Nana', age=18}, Human{name='Amy', age=38}, Human{name='Lili', age=25}] //出列的时候按优先级出列 System.out.println(priorityBlockingQueue.take());//Human{name='Sum', age=9} System.out.println(priorityBlockingQueue.take());//Human{name='Nana', age=18} System.out.println(priorityBlockingQueue.take());//Human{name='Lili', age=25} System.out.println(priorityBlockingQueue.take());//Human{name='Amy', age=38} } } class Human implements Comparable <Human> { String name; int age; public Human() {} public Human(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Human{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Human o) { return this.age-o.age; }
Atas ialah kandungan terperinci Analisis contoh timbunan dan baris gilir Java. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!