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(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 {
System.out.println("正在等待消费");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}
public synchronized WoTou pop() { //产品出栈(消费)
while(index == 0) {
try {
System.out.println("正在等待生产");
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(1000);
} 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(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}以上是我写的一个生产者与消费者的问题,我想给他加一个简单的界面。像这样:不知道输出内容该怎么传到文本框显示,请大家指教。
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(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 {
System.out.println("正在等待消费");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
arrWT[index] = wt;
index ++;
}
public synchronized WoTou pop() { //产品出栈(消费)
while(index == 0) {
try {
System.out.println("正在等待生产");
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(1000);
} 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(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}以上是我写的一个生产者与消费者的问题,我想给他加一个简单的界面。像这样:不知道输出内容该怎么传到文本框显示,请大家指教。
把打印语句改成图形界面中相应的setText()不就行了
是我做的。。用netbean做的
class SyncStack { //栈,用来当容器
int index = 0;
WoTou[] arrWT = new WoTou[6];
//看这里,搞个文本框,然后提供个方法get
//在图形界面代码里调用get方法获得这个文本框,然后把这个文本框加到GUI上
JTextField info = new JTextField();
public JTextField getStackInfoField(){
return info
}
public synchronized void push(WoTou wt) { //产品入栈(生产)
//.......你的代码
//下面加一句
info.setText(index+"");
}
public synchronized WoTou pop() { //产品出栈(消费)
//.......你的代码
//下面加一句
info.setText(index+"");
return arrWT[index];
}
}
我这只是实现一种方法,这个类的信息显示就这么处理了
其它两个类也类似搞个JTextField的成员,然后提供个方法获得
同样的在GUI上添加(注意,你界面上的几个JTextField不再是直接new出来的,而是要用这几个对象获得的)。
至于怎么组织这些东西,你自己先研究研究吧,不懂再来问我。