讲到多线程基础
生产者和消费者就理应拿出来说道说道
生产者与消费者的大致思路如下。
1.当库存为空的时候,消费者进入等待状态,生产者进行生产工作。
2.当库存满的时候,生产者进入等待状态,消费者进行消费。
3.当库存不为空,又不为满的时候,二者争用对象锁,拿锁者,拿天下。
因此,代码思路出来了。
首先定义库存峰值
// 库存峰值
private final static int max = 10;
其次定义当前库存量(初始值为0)
// 当前库存量
private int num = 0;
然后定义锁对象
// 锁对象
private Object obj = new Object();
定义完毕
接下来就是定义生产者和消费者的工作模式了
/**
* 生产者
*
* @param name
*/
public void producer(String name) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) { // 同步锁
synchronized (obj) {
if (num < max) {
System.out.println("生产者:" + name + " " + "正在生产第:" + (num + 1 )+ "个");
num++;
System.out.println("当前库存:NUM=" + num);
// 睡眠1秒
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 线程唤醒
obj.notify();
} else {
System.out.println("库存已满...生产者等待中...");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
t.start();
}
/**
* 消费者
*
* @param name
*/
public void customer(String name) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (obj) {
if (num > 0) {
System.out.println("消费者: " + name + " " + "正在消费第:" + num + "个");
num--;
System.out.println("当前库存:NUM=" + num);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obj.notify();
} else {
System.out.println("仓库已空..正在等待生产...");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
t.start();
}
思路如上所示。
下面是完整代码与测试
package threadtest;
public class ProducerAndCustomer {
// 库存峰值
private final static int max = 10;
// 当前库存量
private int num = 0;
// 锁对象
private Object obj = new Object();
/**
* 生产者
*
* @param name
*/
public void producer(String name) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) { // 同步锁
synchronized (obj) {
if (num < max) {
System.out.println("生产者:" + name + " " + "正在生产第:" + (num + 1 )+ "个");
num++;
System.out.println("当前库存:NUM=" + num);
// 睡眠1秒
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 线程唤醒
obj.notify();
} else {
System.out.println("库存已满...生产者等待中...");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
t.start();
}
/**
* 消费者
*
* @param name
*/
public void customer(String name) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
synchronized (obj) {
if (num > 0) {
System.out.println("消费者: " + name + " " + "正在消费第:" + num + "个");
num--;
System.out.println("当前库存:NUM=" + num);
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obj.notify();
} else {
System.out.println("仓库已空..正在等待生产...");
try {
obj.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
});
t.start();
}
}
测试类
package threadtest;
public class MoreThread{
public static void main(String[] args) {
ProducerAndCustomer pac=new ProducerAndCustomer();
pac.producer("Producer A");
pac.producer("Producer B");
pac.producer("Producer C");
pac.customer("Customer 1");
pac.customer("Customer 2");
pac.customer("Customer 3");
pac.customer("Customer 4");
}
}
测试结果:
测试没问题。