'Develop a multi-threaded application. Don't use synchronized word [duplicate]

Free checkout. The fast food restaurant has several cash desks. Customers stand in line at a particular cash desk, but can move to another queue if the queue decreases or disappears.

In my code, I need to replace the "synchronized" method with some other

start:

package by.naumenka;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Main {
public static void main(String[] args) throws InterruptedException {
List<Cashier> cashiers = new LinkedList<>();
Queue<Customer> customers = new LinkedList<Customer>();

for (int i = 0; i < 3; i++) {
    cashiers.add(new Cashier("Cashier "+ i, customers));
}

for (int i = 0; i < 10; i++) {
      synchronized (customers) {
        customers.add(new Customer("Customer " + i, 1 + (int) (9 * Math.random())));
        customers.notifyAll();
    }
}

synchronized (customers){
    while (!customers.isEmpty()){
        customers.wait();
    }
}

System.out.println("All customers have been served");

}
}

Cashier code:

package by.naumenka;

import java.util.Queue;

public class Cashier extends Thread {

volatile private Queue<Customer> customerQueue;

public Cashier(String name, Queue<Customer> customerQueue) {
super(name);
this.customerQueue = customerQueue;
this.setDaemon(true);
start();
}

@Override
public void run() {
while (true) {
    try {
        Customer currentCustomer = null;
        synchronized (customerQueue) {
            while (customerQueue.size() == 0) {
                customerQueue.wait();
            }
            currentCustomer = customerQueue.poll();
            customerQueue.notifyAll();
        }

        System.out.println(this + " have start to serve " + currentCustomer);
        Thread.sleep(500 * currentCustomer.getTaskQty());
        System.out.println(currentCustomer.getTaskQty()+ " tasks of " + 
currentCustomer + " was served by " + this);

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
}

@Override
public String toString() {
return getName();
}
}

Customer code:

package by.naumenka;

public class Customer  {

private String name;
private int taskQty;

public Customer(String name, int taskQty) {
this.name = name;
this.taskQty = taskQty;
}

public String getName() {
return name;
}

public int getTaskQty() {
return taskQty;
}


@Override
public String toString() {
return getName();
}
}

i tried to use a class that implements BlockingQueue, and the blocking operations put and take. These operations do not require synchronized as they are thread-safe. They also don't require use of wait() and notify.

 package by.naumenka;

 import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.ArrayBlockingQueue;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.TimeUnit;


 public class Main {
 public static void main(String[] args)  {
    BlockingQueue<String> customers = new ArrayBlockingQueue<String>(2);
    List<Cashier> cashiers = new LinkedList<>();

    new Thread(() -> {
        for (int i = 0; i < 3; i++) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
                cashiers.put(new Cashier("Cashier " + i, customers));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(() -> {
        for (int i = 0; i < 10; i++) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
                customers.put(new Customer("Customer " + i, 1 + (int) (9 * 
  Math.random())));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        }).start();

    new Thread(() -> {
    try (customers){
        while (!customers.isEmpty()){
            customers.wait();
        }
    }
    System.out.println("All customers have been served");
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
    }

But my code does not want to work and gives errors, please tell me what I'm doing wrong. I'm not good at programming, but I'm trying



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source