Reputation: 3327
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?
Upvotes: 0
Views: 245
Reputation: 13535
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.
Upvotes: 1
Reputation: 2598
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
Upvotes: 1
Reputation: 1025
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();
}
Upvotes: -1