栈
package com.yuzhenc.collection;
import java.util.Stack;
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);
System.out.println(stack.empty());
System.out.println(stack.peek());
System.out.println(stack);
System.out.println(stack.pop());
System.out.println(stack);
System.out.println(stack.push("E"));
System.out.println(stack);
}
}
队列
阻塞队列
- ArrayBlockingQueue : 不支持读写同时操作,底层基于数组的;
- LinkedBlockingQueue:支持读写同时操作,并发情况下,效率高,底层基于链表;
package com.yuzhenc.collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Test27 {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
System.out.println(arrayBlockingQueue.add("Lili"));
System.out.println(arrayBlockingQueue.offer("Amy"));
arrayBlockingQueue.put("Nana");
arrayBlockingQueue.offer("Daming", 3,TimeUnit.SECONDS);
System.out.println(arrayBlockingQueue);
System.out.println(arrayBlockingQueue.peek());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue);
System.out.println(arrayBlockingQueue.take());
System.out.println(arrayBlockingQueue);
arrayBlockingQueue.clear();
System.out.println(arrayBlockingQueue);
System.out.println(arrayBlockingQueue.peek());
System.out.println(arrayBlockingQueue.poll());
System.out.println(arrayBlockingQueue.poll(2,TimeUnit.SECONDS));
}
}
- SynchronousQueue:方便高效地进行线程间数据的传送,不会产生队列中数据争抢问题;
package com.yuzhenc.collection;
import java.util.concurrent.SynchronousQueue;
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:带优先级的阻塞队列;
- 无界的队列,没有长度限制,但是在你不指定长度的时候,默认初始长度为11,也可以手动指定,当然随着数据不断的加入,底层(底层是数组Object[])会自动扩容,直到内存全部消耗殆尽了,导致 OutOfMemoryError内存溢出 程序才会结束;
- 不可以放入null元素的,不允许放入不可比较的对象(导致抛ClassCastException),对象必须实现内部比较器或者外部比较器;
package com.yuzhenc.collection;
import java.util.concurrent.PriorityBlockingQueue;
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);
System.out.println(priorityBlockingQueue.take());
System.out.println(priorityBlockingQueue.take());
System.out.println(priorityBlockingQueue.take());
System.out.println(priorityBlockingQueue.take());
}
}
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;
}
- DelayQueue:DelayQueue是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走;
- 当生产者线程调用put之类的方法加入元素时,会触发Delayed接口中的compareTo方法进行排序,也就是说队列中元素的顺序是按到期时间排序的,而非它们进入队列的顺序。排在队列头部的元素是最早到期的,越往后到期时间越晚;
- 消费者线程查看队列头部的元素,注意是查看不是取出。然后调用元素的getDelay方法,如果此方法返回的值小0或者等于0,则消费者线程会从队列中取出此元素,并进行处理。如果getDelay方法返回的值大于0,则消费者线程wait返回的时间值后,再从队列头部取出元素,此时元素应该已经到期;
- 不能将null元素放置到这种队列中;
- DelayQueue能做什么
- 淘宝订单业务:下单之后如果三十分钟之内没有付款就自动取消订单;
- 饿了吗订餐通知:下单成功后60s之后给用户发送短信通知;
- 关闭空闲连接。服务器中,有很多客户端的连接,空闲一段时间之后需要关闭之;
- 缓存。缓存中的对象,超过了空闲时间,需要从缓存中移出;
- 任务超时处理。在网络协议滑动窗口请求应答式交互时,处理超时未响应的请求等;
package com.yuzhenc.collection;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
public class Test30 {
DelayQueue<User> dq = new DelayQueue<>();
public void login(User user){
dq.add(user);
System.out.println("用户:[" + user.getId() +"],[" + user.getName() + "]已经登录,预计下机时间为:" + user.getEndTime() );
}
public void logout(){
System.out.println(dq);
try {
User user = dq.take();
System.out.println("用户:[" + user.getId() +"],[" + user.getName() + "]上机时间到,自动退出游戏");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public int onlineSize(){
return dq.size();
}
public static void main(String[] args) {
Test30 test = new Test30();
test.login(new User(1,"张三",System.currentTimeMillis()+5000));
test.login(new User(2,"李四",System.currentTimeMillis()+2000));
test.login(new User(3,"王五",System.currentTimeMillis()+10000));
while(true){
test.logout();
if(test.onlineSize() == 0){
break;
}
}
}
}
class User implements Delayed {
private int id;
private String name;
private long endTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getEndTime() {
return endTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
public User(int id, String name, long endTime) {
this.id = id;
this.name = name;
this.endTime = endTime;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
@Override
public long getDelay(TimeUnit unit) {
return this.getEndTime() - System.currentTimeMillis();
}
@Override
public int compareTo(Delayed o) {
User other = (User)o;
return ((Long)(this.getEndTime())).compareTo((Long)(other.getEndTime()));
}
}
双端队列
package com.yuzhenc.collection;
import java.util.Deque;
import java.util.LinkedList;
public class Test31 {
public static void main(String[] args) {
Deque<String> d = new LinkedList<>() ;
d.offer("A");
d.offer("B");
d.offer("C");
System.out.println(d);
d.offerFirst("D");
d.offerLast("E");
System.out.println(d);
System.out.println(d.poll());
System.out.println(d);
System.out.println(d.pollFirst());
System.out.println(d.pollLast());
System.out.println(d);
}
}
|