I'm working with some low-level multithreading in java where I have two methods produce and consume:
public class Producer {
private LinkedList<Integer> list = new LinkedList();
private final int LIMIT = 10;
private Object lock = new Object();
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (lock) {
// while loopet er til, for at blive ved med at tjekke at tjekke, at listen er fuld
while (list.size() == LIMIT) {
//notify vækker dette while-loop
lock.wait(); //låsen venter indtil der er plads til at blive taget en ny værdi ud
System.out.println("hej");
}
list.add(value++);
lock.notify();
}
}
}
public void consume() throws InterruptedException {
Random random = new Random();
while (true) {
synchronized (lock) {
while (list.size() == 0) {
lock.wait();
}
System.out.print("list size is " + list.size());
int value = list.removeFirst();
System.out.println("Current value is " + value);
lock.notify();
}
Thread.sleep(random.nextInt(1000));
}
}
}
what can I put in the main method for the thread to run? Since I'm in the case is not using Thread of the Runnable interface, I can't start them, and instantiating an object, and calling the methods is not working?