Java线程实现生产和消费的过程,请问如何填写程序片断?
我自己填入代码,但是有错误!
帮帮忙!小弟感激不尽!
class Store
{
public int counts = 0;
}
public class Test implements Runnable
{
int flag;
Store st;
public Test(int flag, Store st)
{
this.flag = flag;
this.st = st;
}
public static void main(String[] args) throws Exception
{
Store mk = new Store();
Test p1 = new Test(0,mk);
Test p3 = new Test(1,mk);
Thread t1 = new Thread(p1);//此行是我填入代码
Thread t3 = new Thread(p3);//此行是我填入代码
t1.start();
t3.start();
}
public void run() {
// TODO Auto-generated method stub
if(this.flag == 0)
{
while(true)
{
modify(0);
try
{
Thread.sleep(2000);
}
catch(Exception ex)
{
}
System.out.println("Thread1"+st.counts);
}
}
else
{
while(true)
{
modify(1);
try
{
Thread.sleep(2000);
}
catch(Exception ex)
{
}
System.out.println("Thread2"+st.counts);
}
}
}
public synchronized void modify(int flag)
{
if(flag == 0)
{
while(st.counts == 5)
{
try
{
wait(); //此行是我填入代码
}
catch(Exception ex){}
}
notify();//此行是我填入代码
st.counts++;
}
else
{
while(st.counts == 0)
{
try
{
wait();//此行是我填入代码
}
catch(Exception ex){}
}
notify();//此行是我填入代码
st.counts--;
}
}
}
我自己填入代码,但是有错误!
帮帮忙!小弟感激不尽!
class Store
{
public int counts = 0;
}
public class Test implements Runnable
{
int flag;
Store st;
public Test(int flag, Store st)
{
this.flag = flag;
this.st = st;
}
public static void main(String[] args) throws Exception
{
Store mk = new Store();
Test p1 = new Test(0,mk);
Test p3 = new Test(1,mk);
Thread t1 = new Thread(p1);//此行是我填入代码
Thread t3 = new Thread(p3);//此行是我填入代码
t1.start();
t3.start();
}
public void run() {
// TODO Auto-generated method stub
if(this.flag == 0)
{
while(true)
{
modify(0);
try
{
Thread.sleep(2000);
}
catch(Exception ex)
{
}
System.out.println("Thread1"+st.counts);
}
}
else
{
while(true)
{
modify(1);
try
{
Thread.sleep(2000);
}
catch(Exception ex)
{
}
System.out.println("Thread2"+st.counts);
}
}
}
public synchronized void modify(int flag)
{
if(flag == 0)
{
while(st.counts == 5)
{
try
{
wait(); //此行是我填入代码
}
catch(Exception ex){}
}
notify();//此行是我填入代码
st.counts++;
}
else
{
while(st.counts == 0)
{
try
{
wait();//此行是我填入代码
}
catch(Exception ex){}
}
notify();//此行是我填入代码
st.counts--;
}
}
}
public class ProducerConsumer {
public static void main(String[] args) {
SyncStack ss = new SyncStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
new Thread(p).start();
new Thread(p).start();
new Thread(p).start();
new Thread(c).start();
}
}class WoTou {
int id;
WoTou(int id) {
this.id = id;
}
public String toString() {
return "WoTou : " + id;
}
}class SyncStack {
int index = 0;
WoTou[] arrWT = new WoTou[6];
public synchronized void push(WoTou wt) {
while(index == arrWT.length) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}
public synchronized WoTou pop() {
while(index == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
return arrWT[index];
}
}class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for(int i=0; i<20; i++) {
WoTou wt = new WoTou(i);
ss.push(wt);
System.out.println("生产了:" + wt);
try {
Thread.sleep((int)(Math.random() * 200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for(int i=0; i<20; i++) {
WoTou wt = ss.pop();
System.out.println("消费了: " + wt);
try {
Thread.sleep((int)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}