#Multithreading, thread never terminates -> infinite waiting time

1 messages · Page 1 of 1 (latest)

ancient meadow
#
BlockingQueue<Integer> multipliedNumbers = new PriorityBlockingQueue<Integer>();
        Thread multiplicationThread = new Thread(new MultByN(exponentialNumbersOf, multipliedNumbers, 2));
        multiplicationThread.start();    
        try {
            multiplicationThread.join();
        } catch (InterruptedException IntExcep) {
            // TODO: handle exception
        }
        Thread consumerThread2 = new Thread(new Print(multipliedNumbers));
        consumerThread2.start();

so this is in my main, as you cann see I start a multiplicationThread, and start it, and I tell the program to wait until the thread finishes, before starting the consumerThread, which sole function is to print the data of the blocking queue. But the consumerThread never starts

This is in my multiplicationThread

@Override
    public void run() {
        try {
            System.out.println("Thread has started");
            while(true) {
                int numberToBeMultipliedByN = exponentialNumbers.take();
                int multiplicatedNumber = numberToBeMultipliedByN*n;
                multiplicationNumbers.add(numberToBeMultipliedByN);
                if(!exponentialNumbers.contains(multiplicatedNumber))
                    multiplicationNumbers.add(multiplicatedNumber);
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
        }
    }

so my output is:

Thread has started

but the printing never starts

snow wraithBOT
# ancient meadow ```java BlockingQueue<Integer> multipliedNumbers = new PriorityBlockingQueue<Int...

Detected code, here are some useful tools:

Formatted code
BlockingQueue<Integer> multipliedNumbers = new PriorityBlockingQueue<Integer>();
Thread multiplicationThread = new Thread(new MultByN(exponentialNumbersOf, multipliedNumbers, 2));
multiplicationThread.start();
try {
  multiplicationThread.join();
} catch (InterruptedException IntExcep) {
  // TODO: handle exception
}
Thread consumerThread2 = new Thread(new Print(multipliedNumbers));
consumerThread2.start();
#

<@&987246399047479336> please have a look, thanks.

shrewd sleet
# ancient meadow ```java BlockingQueue<Integer> multipliedNumbers = new PriorityBlockingQueue<Int...
BlockingQueue<Integer> multipliedNumbers = new PriorityBlockingQueue<Integer>();
        Thread multiplicationThread = new Thread(new MultByN(exponentialNumbersOf, multipliedNumbers, 2));
        multiplicationThread.start();    
        Thread consumerThread2 = new Thread(new Print(multipliedNumbers));
        consumerThread2.start();
        try {
            multiplicationThread.join();
        } catch (InterruptedException IntExcep) {
            // TODO: handle exception
        }
#

join after you started both threads