这个是操作系统中有名的生产者消费者问题...刚写了个实现, 生产者和消费者数量可以自己随便改....
产品也可以扩展...产品类: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();
}}
产品也可以扩展...产品类: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();
}}
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();
}
}
synchronized (变量)