I have written a simple java multithreading program.
I am having some questions regarding the code. Please help me with these questions.
Thanks in advance!
Here is my code:
Producer.java
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 < 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->"+tempmp.p1.getState());
System.out.println("p2->"+tempmp.p2.getState());
System.out.println("p3->"+tempmp.p3.getState());
System.out.println("c1->"+tempmp.c1.getState());
System.out.println("c2->"+tempmp.c2.getState());
System.out.println("c3->"+tempmp.c3.getState());
/*-------------current states---------------------*/
}
}
}
consumer.java
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 < 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->"+tempmp.p1.getState());
System.out.println("p2->"+tempmp.p2.getState());
System.out.println("p3->"+tempmp.p3.getState());
System.out.println("c1->"+tempmp.c1.getState());
System.out.println("c2->"+tempmp.c2.getState());
System.out.println("c3->"+tempmp.c3.getState());
/*-------------current states---------------------*/
}
}
}
DataStorage.java
package com.prodcon;
import java.util.Random;
import java.util.Stack;
import javax.xml.crypto.Data;
public class DataStorage {
int countofdata;
Stack<Double> data;
public DataStorage() {
countofdata = 0;
data = new Stack<Double>();
}
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();
}
}
MainProcess.java
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();
}
}
And heres is the output of this program:
Thread:p2called
Thread:p3called
Current states of the threads:
Thread:p1called
Current states of the threads:
Thread:c3called
Current states of the threads:
p1->RUNNABLE
p2->BLOCKED
p3->RUNNABLE
c1->BLOCKED
c2->BLOCKED
c3->BLOCKED
Thread:p3called
Current states of the threads:
p1->RUNNABLE
p2->BLOCKED
p3->RUNNABLE
c1->BLOCKED
c2->BLOCKED
c3->BLOCKED
Thread:p3called
Thread:c2called
Thread:c1called
Current states of the threads:
Current states of the threads:
p1->RUNNABLE
Current states of the threads:
p2->BLOCKED
p1->RUNNABLE
p2->BLOCKED
p3->BLOCKED
c1->BLOCKED
c2->BLOCKED
c3->BLOCKED
Thread:p1called
Current states of the threads:
p1->RUNNABLE
p2->BLOCKED
p3->BLOCKED
c1->BLOCKED
c2->BLOCKED
c3->BLOCKED
Thread:p1called
Current states of the threads:
p1->RUNNABLE
p2->BLOCKED
p3->BLOCKED
c1->RUNNABLE
c2->RUNNABLE
c3->BLOCKED
Thread:c1called
Current states of the threads:
p1->BLOCKED
p2->BLOCKED
p3->BLOCKED
c1->RUNNABLE
c2->RUNNABLE
c3->BLOCKED
Thread:c1called
Current states of the threads:
p1->BLOCKED
p2->BLOCKED
p3->BLOCKED
c1->RUNNABLE
c2->RUNNABLE
c3->BLOCKED
Current states of the threads:
p1->RUNNABLE
p2->BLOCKED
p3->BLOCKED
c1->TERMINATED
c2->RUNNABLE
c3->BLOCKED
p1->RUNNABLE
p2->BLOCKED
p3->BLOCKED
c1->TERMINATED
c2->BLOCKED
c3->RUNNABLE
Thread:c3called
Current states of the threads:
p1->TERMINATED
p2->BLOCKED
p3->BLOCKED
c1->TERMINATED
c2->BLOCKED
c3->RUNNABLE
Thread:c3called
Current states of the threads:
p1->TERMINATED
p2->BLOCKED
p3->BLOCKED
c1->TERMINATED
c2->BLOCKED
c3->RUNNABLE
p3->RUNNABLE
p1->BLOCKED
p2->RUNNABLE
p3->RUNNABLE
c1->TERMINATED
c2->BLOCKED
c3->TERMINATED
Thread:p2called
Current states of the threads:
p1->TERMINATED
p2->RUNNABLE
p3->RUNNABLE
c1->TERMINATED
c2->BLOCKED
c3->TERMINATED
Thread:p2called
Current states of the threads:
p1->TERMINATED
p2->RUNNABLE
p3->RUNNABLE
c1->TERMINATED
c2->BLOCKED
c3->TERMINATED
p1->TERMINATED
p2->TERMINATED
p3->RUNNABLE
c1->TERMINATED
c2->RUNNABLE
c3->TERMINATED
Thread:c2called
Current states of the threads:
p1->TERMINATED
p2->TERMINATED
p3->RUNNABLE
c1->TERMINATED
c2->RUNNABLE
c3->TERMINATED
Thread:c2called
Current states of the threads:
p1->TERMINATED
p2->TERMINATED
p3->RUNNABLE
c1->TERMINATED
c2->RUNNABLE
c3->TERMINATED
c1->TERMINATED
c2->TERMINATED
c3->TERMINATED
My questions are :
1.According to the program this process should never stop...but then some threads are automatically getting terminated why??
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?
3.According to this code : What is the difference between the producer-consumer problem and reader-writer problem?
Thanks again!!