Note that there are some explanatory texts on larger screens.

plurals
  1. POProducer-consumers(many). Consumers take and put into the shared queue
    primarykey
    data
    text
    <p>I made a producer-consumer program. It's just a program in core java without any GUI(Swing or SWT). It has one producer who put objects into the queue. Also there is a few consumers who must add some staff(for example String) into Every object in that shared queue. So, every consumer must handle every object in a shared queue. In this case - every BookShelf must have items from All consumers in "books" ArrayList. consumers. </p> <p><strong>Question:</strong> What condition should I use in consumers to finish their threads correctly? Here are the code fragments of the program. Maybe I implemented it in wrong way.</p> <p>Here is an object for the queue:</p> <pre><code>public class BookShelf { private int id; private String name; private int height; private int weigh; List&lt;String&gt; books = Collections.synchronizedList(new ArrayList&lt;String&gt;()); public BookShelf(int id, String name) { this.id = id; this.name = name; } public void addBook(String book) { books.add(book); } public boolean eq(String book) { synchronized (books) { for (String b: books) { if (b.equalsIgnoreCase(book)) { return true; } } } return false; } other setters and getters.. </code></pre> <p>}</p> <p>Here is the producer class:</p> <pre><code>public class Producer implements Runnable { private BlockingQueue myQueue; public Producer(BlockingQueue myQueue) { this.myQueue = myQueue; } public void run() { for(int i=0; i&lt;7; i++){ try { System.out.println("Produced: " + i); BookShelf myBookShelf = new BookShelf(i, "book #" + i); myQueue.put(myBookShelf); } catch (InterruptedException ex) { //Proper handle } } } </code></pre> <p>}</p> <p>Here is one of consumers class:</p> <pre><code> public class Consumer implements Runnable { private BlockingQueue myQueue; public Consumer(BlockingQueue myQueue) { this.myQueue = myQueue; } public void run() { while(true){ try { BookShelf tempBookShelf = (BookShelf) myQueue.take(); //eq() is my method to check if ArraList has a book. if (tempBookShelf.eq("Abc book")) { System.out.println("It already has book"); myQueue.put(tempBookShelf); Thread.sleep(2000); } else { tempBookShelf.addBook("Abc book"); myQueue.put(tempBookShelf); Thread.sleep(2000); } } catch (InterruptedException ex) { //Proper handle } } } } </code></pre> <p>Here is main class:</p> <pre><code>public class ProducerConsumerTest { public static void main(String[] args) { BlockingQueue sharedQueue = new LinkedBlockingQueue(); Thread prodThread = new Thread(new Producer(sharedQueue)); Thread consThread = new Thread(new Consumer(sharedQueue)); Thread consThread2 = new Thread(new Consumer2(sharedQueue)); prodThread.start(); consThread.start(); consThread2.start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload