Repository navigation
Expand file tree
/
Copy pathQueueExample.java
More file actions
63 lines (50 loc) · 1.71 KB
/
Copy pathQueueExample.java
File metadata and controls
63 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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<Person2> 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 +
'}';
}
}