0

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?

Kristoffer Tølbøll
  • 3,157
  • 5
  • 34
  • 69

3 Answers3

1

You can use Anonymous Threads for doing this.

public static void main(String args[]) throws IOException, SaxonApiException {
    Producer producer = new Producer();
    new Thread()
    {
        public void run() {
            try {
                producer.consume();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();

    new Thread()
    {
        public void run() {

            try {
                producer.produce();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

And what I am getting in the output is this.

list size is 1Current value is 0
list size is 10Current value is 1
hej
list size is 10Current value is 2
hej
list size is 10Current value is 3
hej
list size is 10Current value is 4
hej
Gatusko
  • 2,503
  • 1
  • 17
  • 25
1

I assume both methods are in class Producer. No other classes are necessary.

public static void main(String... args) {
    Producer producer = new Producer();
    Thread t1 = new Thread(producer::produce);
    Thread t2 = new Thread(producer::consume);
    t1.start(); t2.start();
}

But first throws InterruptedException must be removed from the signatures of produce and consume methods. Throwing exception from the root method of a thread has no sense anyway, because there is no caller who can catch and react to that exception. Just catch the exception inside the methods, print stacktrace and return.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38
-1

To be able to run your methods simultaneously you will need to implement some variant of the thread class / runnable abstract as follows:

// Thread variant
class MultithreadingObject extends Thread{
    public void run(){
        print("...");
    }
} 

public static void main(string[] args){
   threadOne = new MultithreadingObject();
   threadTwo = new MultithreadingObject();
   // Run both threads
   threadOne.start();
   threadTwo.start();
}

Implements Runnable variant:

public class MyThread extends Thread {
    public MyThread() {
        super("MyThread");
    }
    public void run() {
        //Code
    }
}
public static void main(string[] args){
  threadOne = new MyThread();
  threadTwo = new MyThread();
  // Run both threads
  threadOne.start();
  threadTwo.start();
}
Philip DiSarro
  • 1,007
  • 6
  • 9