Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- class CONST
- {
- public static int n = 10;
- public static List<Integer> list = new ArrayList<>();
- public static int rem;
- }
- public class Labfour2 {
- public static void main(String[] args) {
- BlockingQueue queue = new BlockingQueue();
- Squares task1 = new Squares(queue);
- PrintSquares task2 = new PrintSquares(queue);
- new Thread(task1).start();
- new Thread(task2).start();
- }
- }
- // Класс BlockingQueue, хранящий очередь
- class BlockingQueue{
- private int product=0;
- // private int crt=0;
- public synchronized void get() {
- while (product<1) {
- try {
- wait();
- }
- catch (InterruptedException e) {
- }
- }
- product--;
- // System.out.println(CONST.list.get(0));
- CONST.rem = CONST.list.get(0);
- CONST.list.remove(0);
- notify();
- }
- public synchronized void put(int value) {
- while (product>=CONST.n) {
- try {
- wait();
- }
- catch (InterruptedException e) {
- }
- }
- product++;
- CONST.list.add(value);
- // crt++;
- notify();
- }
- }
- // класс Производитель
- class Squares implements Runnable{
- BlockingQueue store;
- Squares(BlockingQueue store){
- this.store=store;
- }
- public void run(){
- for (int i = 0; i <= CONST.n; i++) {
- store.put(i*i);
- }
- }
- }
- // Класс Потребитель
- class PrintSquares implements Runnable{
- BlockingQueue store;
- PrintSquares(BlockingQueue store){
- this.store=store;
- }
- public void run(){
- for (int i = 0; i < CONST.n; i++) {
- store.get();
- System.out.println(CONST.rem);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement