Note that there are some explanatory texts on larger screens.

plurals
  1. POJava multithreading queries on Producer Consumer application
    primarykey
    data
    text
    <p>I have written a simple java multithreading program.</p> <p>I am having some questions regarding the code. Please help me with these questions.</p> <p>Thanks in advance!</p> <p>Here is my code:</p> <p>Producer.java</p> <pre><code>package com.prodcon; import java.util.Stack; public class Producer extends Thread { DataStorage data; MainProcess tempmp; public Producer(DataStorage dst, MainProcess mp){ data = dst; tempmp = mp; } public void run(){ for(int i = 0; i &lt; 3; i++){ System.out.println("Thread:"+this.getName()+"called"); data.PutData(); /*-------------current states---------------------*/ System.out.println("Current states of the threads:"); System.out.println("p1-&gt;"+tempmp.p1.getState()); System.out.println("p2-&gt;"+tempmp.p2.getState()); System.out.println("p3-&gt;"+tempmp.p3.getState()); System.out.println("c1-&gt;"+tempmp.c1.getState()); System.out.println("c2-&gt;"+tempmp.c2.getState()); System.out.println("c3-&gt;"+tempmp.c3.getState()); /*-------------current states---------------------*/ } } } </code></pre> <p>consumer.java</p> <pre><code> package com.prodcon; public class Consumer extends Thread { DataStorage data; MainProcess tempmp; public Consumer(DataStorage dst, MainProcess mp){ data = dst; tempmp = mp; } public void run(){ for(int i = 0; i &lt; 3; i++){ System.out.println("Thread:"+this.getName()+"called"); data.GetData(); /*-------------current states---------------------*/ System.out.println("Current states of the threads:"); System.out.println("p1-&gt;"+tempmp.p1.getState()); System.out.println("p2-&gt;"+tempmp.p2.getState()); System.out.println("p3-&gt;"+tempmp.p3.getState()); System.out.println("c1-&gt;"+tempmp.c1.getState()); System.out.println("c2-&gt;"+tempmp.c2.getState()); System.out.println("c3-&gt;"+tempmp.c3.getState()); /*-------------current states---------------------*/ } } } </code></pre> <p>DataStorage.java</p> <pre><code> package com.prodcon; import java.util.Random; import java.util.Stack; import javax.xml.crypto.Data; public class DataStorage { int countofdata; Stack&lt;Double&gt; data; public DataStorage() { countofdata = 0; data = new Stack&lt;Double&gt;(); } public synchronized void GetData() { 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++; notifyAll(); } public synchronized void PutData() { while (true) { if (data.size() == 3) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { break; } } double temp = Math.random(); data.push(temp); //System.out.println("Data inserted in storage:" + temp); countofdata--; notifyAll(); } } </code></pre> <p>MainProcess.java</p> <pre><code> package com.prodcon; public class MainProcess { /** * @param args */ DataStorage ProcessData; public Producer p1, p2, p3, p4; public Consumer c1, c2, c3, c4; public MainProcess(){ ProcessData = new DataStorage(); p1 = new Producer(ProcessData, this); p2 = new Producer(ProcessData, this); p3 = new Producer(ProcessData, this); c1 = new Consumer(ProcessData, this); c2 = new Consumer(ProcessData, this); c3 = new Consumer(ProcessData, this); p1.setName("p1"); p2.setName("p2"); p3.setName("p3"); c1.setName("c1"); c2.setName("c2"); c3.setName("c3"); } public void startprocess(){ p1.start(); p2.start(); p3.start(); c1.start(); c2.start(); c3.start(); } public static void main(String[] args) { // TODO Auto-generated method stub MainProcess mp1 = new MainProcess(); mp1.startprocess(); } } </code></pre> <p>And heres is the output of this program:</p> <pre><code> Thread:p2called Thread:p3called Current states of the threads: Thread:p1called Current states of the threads: Thread:c3called Current states of the threads: p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;RUNNABLE c1-&gt;BLOCKED c2-&gt;BLOCKED c3-&gt;BLOCKED Thread:p3called Current states of the threads: p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;RUNNABLE c1-&gt;BLOCKED c2-&gt;BLOCKED c3-&gt;BLOCKED Thread:p3called Thread:c2called Thread:c1called Current states of the threads: Current states of the threads: p1-&gt;RUNNABLE Current states of the threads: p2-&gt;BLOCKED p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;BLOCKED c2-&gt;BLOCKED c3-&gt;BLOCKED Thread:p1called Current states of the threads: p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;BLOCKED c2-&gt;BLOCKED c3-&gt;BLOCKED Thread:p1called Current states of the threads: p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;RUNNABLE c2-&gt;RUNNABLE c3-&gt;BLOCKED Thread:c1called Current states of the threads: p1-&gt;BLOCKED p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;RUNNABLE c2-&gt;RUNNABLE c3-&gt;BLOCKED Thread:c1called Current states of the threads: p1-&gt;BLOCKED p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;RUNNABLE c2-&gt;RUNNABLE c3-&gt;BLOCKED Current states of the threads: p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;TERMINATED c2-&gt;RUNNABLE c3-&gt;BLOCKED p1-&gt;RUNNABLE p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;RUNNABLE Thread:c3called Current states of the threads: p1-&gt;TERMINATED p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;RUNNABLE Thread:c3called Current states of the threads: p1-&gt;TERMINATED p2-&gt;BLOCKED p3-&gt;BLOCKED c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;RUNNABLE p3-&gt;RUNNABLE p1-&gt;BLOCKED p2-&gt;RUNNABLE p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;TERMINATED Thread:p2called Current states of the threads: p1-&gt;TERMINATED p2-&gt;RUNNABLE p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;TERMINATED Thread:p2called Current states of the threads: p1-&gt;TERMINATED p2-&gt;RUNNABLE p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;BLOCKED c3-&gt;TERMINATED p1-&gt;TERMINATED p2-&gt;TERMINATED p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;RUNNABLE c3-&gt;TERMINATED Thread:c2called Current states of the threads: p1-&gt;TERMINATED p2-&gt;TERMINATED p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;RUNNABLE c3-&gt;TERMINATED Thread:c2called Current states of the threads: p1-&gt;TERMINATED p2-&gt;TERMINATED p3-&gt;RUNNABLE c1-&gt;TERMINATED c2-&gt;RUNNABLE c3-&gt;TERMINATED c1-&gt;TERMINATED c2-&gt;TERMINATED c3-&gt;TERMINATED </code></pre> <p>My questions are :</p> <p>1.According to the program this process should never stop...but then some threads are automatically getting terminated why??</p> <p>2.Even after asking some threads to go in wait state ..No thread is going in wait state according to the output. why and how?</p> <p>3.According to this code : What is the difference between the producer-consumer problem and reader-writer problem?</p> <p>Thanks again!!</p>
    singulars
    1. This table or related slice is empty.
    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.
    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