这个是操作系统中有名的生产者消费者问题...刚写了个实现, 生产者和消费者数量可以自己随便改....
产品也可以扩展...产品类:package selfimpr.producerCustomer;/**
 * 产品
 * @author selfimpr
 * @blog http://blog.csdn.net/lgg201
 * @email [email protected]
 *
 */
public class Product {
private int id;
private String name;

public Product(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Product: {id: " + this.id + ", name: " + this.name + "};";
}
}
生产者类:package selfimpr.producerCustomer;import java.util.List;/**
 * 生产者线程, 将database实现
 * @author selfimpr
 * @blog http://blog.csdn.net/lgg201
 * @email [email protected]
 *
 */
public class Producer implements Runnable { private List<Product> database;
private static int count = 0;
private int sn; public Producer(int sn, List<Product> database) {
this.sn = sn;
this.database = database;
} @Override
public void run() {
while (true) {
if (database.size() < 10) {
produce();
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} private void produce() {
synchronized (database) {
Product product = new Product(count, "product_" + count++);
database.add(product);
System.out.println("Producer[" + this.sn + "] produce " + product);
}
}}消费者类:package selfimpr.producerCustomer;import java.util.List;/**
 * 消费者线程, 同步database
 * @author selfimpr
 * @blog http://blog.csdn.net/lgg201
 * @email [email protected]
 *
 */
public class Customer implements Runnable {
private List<Product> database;
private int sn; public Customer(int sn, List<Product> database) {
this.sn = sn;
this.database = database;
} @Override
public void run() {
while (true) {
if (database.size() > 0) {
custom();
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} private void custom() {
synchronized (database) {
if (database.size() > 0) {
Product product = database.remove(0);
System.err.println("Customer[" + this.sn + "] custom "
+ product);
}
}
}
}主程序:package selfimpr.producerCustomer;import java.util.ArrayList;
import java.util.List;/**
 * 运行入口
 * @author selfimpr
 * @blog http://blog.csdn.net/lgg201
 * @email [email protected]
 *
 */
public class Main { public static void main(String[] args) {
List<Product> database = new ArrayList<Product>();
Thread producer_1 = new Thread(new Producer(1, database));
Thread producer_2 = new Thread(new Producer(2, database));
Thread customer_1 = new Thread(new Customer(1, database));
Thread customer_2 = new Thread(new Customer(2, database));
Thread customer_3 = new Thread(new Customer(3, database));

producer_1.start();
producer_2.start();
customer_1.start();
customer_2.start();
customer_3.start();
}}

解决方案 »

  1.   

    class Producer implements Runnable {    private SyncStack stack;    public Producer(SyncStack stack) {        this.stack = stack;    }    public void run() {        for (int i = 0; i < stack.getProducts().length; i++) {            String product = "产品" + i;            stack.push(product);            System.out.println("生产了: " + product);            try {                Thread.sleep(200);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}class Consumer implements Runnable {    private SyncStack stack;    public Consumer(SyncStack stack) {        this.stack = stack;    }    public void run() {        for (int i = 0; i < stack.getProducts().length; i++) {            String product = stack.pop();            System.out.println("消费了: " + product);            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }}class SyncStack {    private String[] products = new String[10];    private int index;    public synchronized void push(String product) {        if (index == product.length()) {            try {                wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        notify();        products[index] = product;        index++;    }    public synchronized String pop() {        if (index == 0) {            try {                wait();            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }        notify();        index--;        String product = products[index];        return product;    }    public String[] getProducts() {        return products;    }}public class TestProducerConsumer {    public static void main(String[] args) {        SyncStack stack = new SyncStack();        Producer p = new Producer(stack);        Consumer c = new Consumer(stack);        new Thread(p).start();        new Thread(c).start();    }}这是1个人的
      

  2.   

    感到楼主这个要求用ArrayBlockingQueue会很容易实现,大部分的工作都被JDK给做了。实例代码如下:import java.util.concurrent.ArrayBlockingQueue;public class Producer implements Runnable {

    private ArrayBlockingQueue<String> queue;
    private String producerName;

    public Producer(String producerName, ArrayBlockingQueue<String> queue) {
    this.queue = queue;
    this.producerName = producerName;
    } @Override
    public void run() {
    while(true){
    try {
    this.queue.put("Produced by " + this.producerName);
    System.out.println("Number of products in the queue after production: " + this.queue.size());
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }}import java.util.concurrent.ArrayBlockingQueue;public class Consumer implements Runnable {

    private ArrayBlockingQueue<String> queue;

    public Consumer(ArrayBlockingQueue<String> queue) {
    this.queue = queue;
    } @Override
    public void run() {
    while(true){
    try {
    this.queue.take();
    System.out.println("Number of products in the queue after consumption: " + this.queue.size());
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }}import java.util.concurrent.ArrayBlockingQueue;public class ProducerConsumerTest { public static void main(String[] args) {

    ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);

    Producer producer1 = new Producer("producer1", queue);
    Producer producer2 = new Producer("producer2", queue);
    Producer producer3 = new Producer("producer2", queue);

    Consumer consumer1 = new Consumer(queue);
    Consumer consumer2 = new Consumer(queue);
    Consumer consumer3 = new Consumer(queue);

    new Thread(producer1).start();
    new Thread(producer2).start();
    new Thread(producer3).start();
    new Thread(consumer1).start();
    new Thread(consumer2).start();
    new Thread(consumer3).start();
    }
    }
      

  3.   

    关键注意 线程等待和线程唤醒的应用wait 和 notify 的用法
      

  4.   

    对了一定要加线程同步
    synchronized (变量)
      

  5.   

    如 5 楼所说的,用 BlockingQueue 吧,可以不用考虑同步,并且会自动形成阻塞。
      

  6.   

    队列加上 synchronizatied,其他随便搞