import java.io.*;
import java.util.*;
abstract class Person {
protected static boolean estate = false;
protected abstract void make(); protected abstract void shopping(); protected boolean getEstate() {
return estate;
} protected void setEstate(boolean value) {
estate = value;
}
} class ProducerAndConsumer {
public ProducerAndConsumer() {
} public static void main(String[] args) {
LinkedList name = new LinkedList();
Thread fill = new Thread(new Producer(name));
Thread clear = new Thread(new Consumer(name));
fill.start();
clear.start();
}
}class Producer
extends Person implements Runnable {
private List container;
Producer(List container) {
this.container = container;
}
public void make() {
DataInputStream buffer = null;
try {
buffer = new DataInputStream(new BufferedInputStream(System.in));
container.add("ON:" + container.size() + " Enter: " + buffer.readLine());
if (container.size() == 10) { this.setEstate(true);
}
} catch (Exception er) {
er.printStackTrace();
}
finally {
/* if free here thread stream exception ...why? seem becuase System.in open tow */
// try {
// buffer.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
}
} public void shopping() {} public void run() {
while (true) {
synchronized (container) { try {
if (this.getEstate()) {
container.notifyAll(); container.wait(); } this.make();
}
catch (InterruptedException ex) {
}
} } }
}class Consumer
extends Person implements Runnable {
private List container;
private boolean isNull;
Consumer(List container) {
this.container = container;
} public void make() {} public void shopping() {
ListIterator it = container.listIterator();
System.out.println(it.next());
it.remove();
try{Thread.sleep(1000);}catch(Exception er){er.printStackTrace();}
if(container.isEmpty())
this.setEstate(false);
} public void run() {
while (true) {
synchronized (container) {
try {
if (!this.getEstate()) {
container.notifyAll(); container.wait();
}
this.shopping(); }
catch (Exception er) {
er.printStackTrace();
}
} }
}
}
import java.util.*;
abstract class Person {
protected static boolean estate = false;
protected abstract void make(); protected abstract void shopping(); protected boolean getEstate() {
return estate;
} protected void setEstate(boolean value) {
estate = value;
}
} class ProducerAndConsumer {
public ProducerAndConsumer() {
} public static void main(String[] args) {
LinkedList name = new LinkedList();
Thread fill = new Thread(new Producer(name));
Thread clear = new Thread(new Consumer(name));
fill.start();
clear.start();
}
}class Producer
extends Person implements Runnable {
private List container;
Producer(List container) {
this.container = container;
}
public void make() {
DataInputStream buffer = null;
try {
buffer = new DataInputStream(new BufferedInputStream(System.in));
container.add("ON:" + container.size() + " Enter: " + buffer.readLine());
if (container.size() == 10) { this.setEstate(true);
}
} catch (Exception er) {
er.printStackTrace();
}
finally {
/* if free here thread stream exception ...why? seem becuase System.in open tow */
// try {
// buffer.close();
// } catch (IOException ex) {
// ex.printStackTrace();
// }
}
} public void shopping() {} public void run() {
while (true) {
synchronized (container) { try {
if (this.getEstate()) {
container.notifyAll(); container.wait(); } this.make();
}
catch (InterruptedException ex) {
}
} } }
}class Consumer
extends Person implements Runnable {
private List container;
private boolean isNull;
Consumer(List container) {
this.container = container;
} public void make() {} public void shopping() {
ListIterator it = container.listIterator();
System.out.println(it.next());
it.remove();
try{Thread.sleep(1000);}catch(Exception er){er.printStackTrace();}
if(container.isEmpty())
this.setEstate(false);
} public void run() {
while (true) {
synchronized (container) {
try {
if (!this.getEstate()) {
container.notifyAll(); container.wait();
}
this.shopping(); }
catch (Exception er) {
er.printStackTrace();
}
} }
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货