生产者消费者问题之Java线程实现
原贴内容请见: http://topic.csdn.net/u/20100116/17/6a204504-9e70-435f-be55-f0630ed12fdf.html?seed=942806558&r=62778073#r_62778073
请大牛指正.
产品类:
package selfimpr.producerCustomer;
/**
* 产品
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*
*/
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 lgg860911@yahoo.com.cn
*
*/
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 lgg860911@yahoo.com.cn
*
*/
public class Customer implements Runnable {
private List<Product> database;
private int sn;
public Cus
相关文档:
http://www.blogjava.net/flysky19/articles/93492.html
1.java project环境,使用java.io用相对路径读取文件的例子:
*目录结构:
DecisionTree
|___src
  ......
Java常用字符集编码详解
Web开发的时候经常会遇到一些字符编码的错误,如页面乱码等问题,所以有必要需对字符编码有所了解,以下是Ricki收集的一些资料(可能不是很全,但希望对你有所帮助)
Java标准字符集:所谓Java标准字符集,就是Java平台支持的字符 ......
源代码:
public class Parent
{
protected void test() {}
public Parent()
{
this.test();
}
public static void main(String[] args)
&nb ......