package queue;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
public class QueueExample {
public static void main(String[] args) {
example1();
}
public static void example1()
{
// оÑеÑÐµÐ´Ñ FIFO (first in first out)
Queue people = new ArrayBlockingQueue<>(4); // 4 - Ð»Ð¸Ð¼Ð¸Ñ Ð²Ð¼ÐµÑÑиÑелÑноÑÑи оÑеÑеди
people.add(new Person2(2));
people.add(new Person2(3));
people.add(new Person2(1));
people.add(new Person2(4));
System.out.println(people.remove()); // веÑнÑл и Ñдалил
System.out.println(people.peek()); // веÑнÑл без ÑдалениÑ
System.out.println(people);
people.add(new Person2(5));
try {
people.add(new Person2(6));
} catch (IllegalStateException e) {
System.out.println("ÐÐ»ÐµÐ¼ÐµÐ½Ñ Ð½Ðµ добавлен, по пÑиÑине " + e.getMessage());
}
boolean result = people.offer(new Person2(6)); // ÑÐ»ÐµÐ¼ÐµÐ½Ñ Ñоже не добавлен, но без exception
System.out.println(result);
System.out.println(people);
/*
ÐÑвод:
Person2{id=2}
Person2{id=3}
[Person2{id=3}, Person2{id=1}, Person2{id=4}]
ÐÐ»ÐµÐ¼ÐµÐ½Ñ Ð½Ðµ добавлен, по пÑиÑине Queue full
false
[Person2{id=3}, Person2{id=1}, Person2{id=4}, Person2{id=5}]
*/
}
}
class Person2 {
private int id;
public Person2(int id) {
this.id = id;
}
@Override
public String toString() {
return "Person2{" +
"id=" + id +
'}';
}
}