Question

I am Creating 2 Classes with two different threads. And passing queue value from thread 1 to Thread 2, But i am not getting anything.

Thread 1

public class Thread1 implements Runnable {
    BlockingQueue queue = new LinkedBlockingQueue<>();

    public void run() {
        for(int i=0; i<=10; i++) {
            try {
                queue.put(i+2);
                System.out.println("Thread 1");
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

Thread 2

public class Thred2  implements Runnable {
    Thread1 Thread1 = new Thread1();
    public void run() {
        for(int i=0; i<=10; i++) {
            try {
                System.out.println("Thread 2" + Thread1.queue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Thred2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

Main Function

public class Test {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Thread1());
        Thread t2 = new Thread(new Thred2());
        t1.start();
        t2.start();
    }
}
Was it helpful?

Solution

In your code, you are creating a Thread1 object within the main method, and another Thread1 object for the one instance of Thred2. These two Thread1 objects are independent. In particular, each of the two objects has its own LinkedBlockingQueue.

You have to change your code in such a way, that Thread1 and Thred2 uses the same LinkedBlockingQueue. For example in the following way:

I am Creating 2 Classes with two different threads. And passing queue value from thread 1 to Thread 2, But i am not getting anything.

public class Thread1 implements Runnable {
    private final BlockingQueue queue;
    public Thread1(BlockingQueue queue) { this.queue = queue; }
    public void run() {
        for(int i=0; i<=10; i++) {
            try {
                queue.put(i+2);
                System.out.println("Thread 1");
            } catch (InterruptedException ex) {
                Logger.getLogger(Thread1.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

public class Thred2  implements Runnable {
    private final BlockingQueue queue;
    public Thred2(BlockingQueue queue) { this.queue = queue; }
    public void run() {
        for(int i=0; i<=10; i++) {
            try {
                System.out.println("Thread 2" + queue.take());
            } catch (InterruptedException ex) {
                Logger.getLogger(Thred2.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

public class Test {
    public static void main(String[] args) {
        BlockingQueue queue = new LinkedBlockingQueue<>();
        Thread t1 = new Thread(new Thread1(queue));
        Thread t2 = new Thread(new Thred2(queue));
        t1.start();
        t2.start();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top