Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First&amp;second questions - this code naturally exits because of the logic. Try adding a debug output here to see when producers quit. </p> <pre><code>if (data.size() == 3) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { System.out.printf("%s: breaking as data size is %d, %d%n", Thread.currentThread().getName(), countofdata, data.size()); break; } </code></pre> <p>Another possible problem is: </p> <pre><code>for(int i = 0; i &lt; 3; i++){ data.PutData(); } </code></pre> <p>You put only three times into the queue.</p> <p>Hint: classy implementation of the producer-consumer problem in Java is i.e. <code>LinkedBlockingQueue</code>.</p> <p>Third question. I'll try to explain this with producers and consumers. Reader-writer problem is when you don't have producers, but have many consumers which access a single shared resource. Some of these can read the resource and some can write to the resource. Simultaneous reads are possible and the write lock is exclusive. </p> <p><strong>UPDATE</strong></p> <p>To emulate multiple reads, you may try using </p> <ul> <li><code>LinkedBlockingQueue</code> is a well-perfomant equivalent for your <code>DataStorage</code>.</li> <li><code>ReentrantReadWriteLock</code></li> <li>Or update your solution. This is study example and I didn't test, but should give you the general idea:</li> </ul> <pre>final Object readLock = new Object(); //use objects as locks final Object writeLock = new Object();</pre> <pre><code>public void GetData() { synchronized (readLock) { // acquire only the read lock while (data.isEmpty()) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block } } double temp = (double) data.pop(); //System.out.println("Data poped out:" + temp); countofdata++; System.out.printf("%s: %d, %d%n", Thread.currentThread().getName(), countofdata, data.size()); notifyAll(); } } public void PutData() { synchronized (readLock) { //first, acquire the read lock synchronized (writeLock) { // then acquire the write lock while (true) { if (data.size() == 3) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { System.out.printf("%s: breaking as data size is %d, %d%n", Thread.currentThread().getName(), countofdata, data.size()); break; } } double temp = Math.random(); data.push(temp); //System.out.println("Data inserted in storage:" + temp); countofdata--; notifyAll(); } } } </code></pre>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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