前面的代码格式不太好,又整理了一下。import java.util.ArrayList;
import java.util.List;
/*
* Created on 2004-4-1
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author Dingding
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class ThreadModel extends Thread {
public static void main(String[] args) {
List store = new ArrayList();
Producer p = new Producer(store);
//Set higher priority.
p.setPriority(Thread.MAX_PRIORITY); p.start();
Consumer[] c = new Consumer[5];
for (int i = 0; i < 5; ++i) {
c[i] = new Consumer(store);
c[i].start();
}
}
}
class Consumer extends Thread {
private List store;
public Consumer(List store) {
this.store = store;
this.ID = count++;
}
public synchronized void consume() {
// synchronized (store) {
while (store.isEmpty()) {
try {
System.out.println("Consumer[" + ID
+ "]:\tNo products available.");
store.notifyAll();
store.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Object obj = store.get(0);
store.remove(0);
System.out.println("Consumer[" + ID + "]:\t" + obj + " consumed.");
// }
}
public void run() {
while (live) {
int amount = (int)(Math.random() * CAPICITY);
for(int i = 0; i < amount; ++i) {
consume();
}
try {
Thread.sleep(100 * amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static final int CAPICITY = 10;
private static int count = 0;
private int ID = 0;
private boolean live = true;
}
class Producer extends Thread {
private List store;
public Producer(List store) {
this.store = store;
}
public synchronized void supply() {
// synchronized (store) {
while (store.size() > CAPICITY) {
try {
System.out.println("Producer:\tNo space left in store.");
store.notifyAll();
store.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = (int) (Math.random() * 10000);
Integer obj = new Integer(i);
store.add(obj);
System.out.println("Producer:\t" + obj + " produced");
// }
}
public void run() {
while (live) {
int amount = (int)(Math.random() * CAPICITY);
for(int i = 0; i < amount; ++i) {
supply();
}
try {
Thread.sleep(8 * amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static final int CAPICITY = 50;
private boolean live = true;
}
import java.util.List;
/*
* Created on 2004-4-1
*
* TODO To change the template for this generated file go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
/**
* @author Dingding
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class ThreadModel extends Thread {
public static void main(String[] args) {
List store = new ArrayList();
Producer p = new Producer(store);
//Set higher priority.
p.setPriority(Thread.MAX_PRIORITY); p.start();
Consumer[] c = new Consumer[5];
for (int i = 0; i < 5; ++i) {
c[i] = new Consumer(store);
c[i].start();
}
}
}
class Consumer extends Thread {
private List store;
public Consumer(List store) {
this.store = store;
this.ID = count++;
}
public synchronized void consume() {
// synchronized (store) {
while (store.isEmpty()) {
try {
System.out.println("Consumer[" + ID
+ "]:\tNo products available.");
store.notifyAll();
store.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Object obj = store.get(0);
store.remove(0);
System.out.println("Consumer[" + ID + "]:\t" + obj + " consumed.");
// }
}
public void run() {
while (live) {
int amount = (int)(Math.random() * CAPICITY);
for(int i = 0; i < amount; ++i) {
consume();
}
try {
Thread.sleep(100 * amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static final int CAPICITY = 10;
private static int count = 0;
private int ID = 0;
private boolean live = true;
}
class Producer extends Thread {
private List store;
public Producer(List store) {
this.store = store;
}
public synchronized void supply() {
// synchronized (store) {
while (store.size() > CAPICITY) {
try {
System.out.println("Producer:\tNo space left in store.");
store.notifyAll();
store.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int i = (int) (Math.random() * 10000);
Integer obj = new Integer(i);
store.add(obj);
System.out.println("Producer:\t" + obj + " produced");
// }
}
public void run() {
while (live) {
int amount = (int)(Math.random() * CAPICITY);
for(int i = 0; i < amount; ++i) {
supply();
}
try {
Thread.sleep(8 * amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static final int CAPICITY = 50;
private boolean live = true;
}
你要的原代码我发你邮箱了
是[email protected]
看下我的代码,不知对楼主是否有帮助class Q {
int n;
boolean valueset=false;
synchronized int get(){
if(!valueset)
try{
wait();
}catch(java.lang.InterruptedException e){
System.out.println("interruption");
}
System.out.println("got: "+n);
valueset=false;
notify();
return n;
}
synchronized void put(int n){
if(valueset)
try{
wait();
}catch(java.lang.InterruptedException e){
System.out.println("interruption caught");
}
this.n=n;
valueset=true;
System.out.println("put:"+n);
notify();
}
}
class producer implements java.lang.Runnable{
Q q;
producer(Q q){
this.q=q;
new Thread(this,"producer").start();
}
public void run(){
int i=0;
while(true)
q.put(i++);
}
}
class consumer implements java.lang.Runnable{
Q q;
consumer(Q q){
this.q=q;
new Thread(this,"consumer").start();
}
public void run(){
while(true)
q.get();
}
}class PCFixed {
public static void main(String arg[]){
Q q=new Q();
new producer(q);
new consumer(q);
System.out.println("press ctrl+c to stop");
}
}