package test2;public class TestProductAndConsumer { public static void main(String[] args) {
Basket b = new Basket();
new Thread(new Customer(b)).start();
new Thread(new Producer(b)).start();
}
}//生产者
class Customer implements Runnable {
private Basket b; public Customer() {
}; public Customer(Basket b) {
this.b = b;
} public void run() {
try {
while (true) {
if (b.i == 0) {
this.wait();
}
b.getG();
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//消费者
class Producer implements Runnable {
private Basket b; public Producer() {
}; public Producer(Basket b) {
this.b = b;
} public void run() {
try {
while (true) {
if (b.i == b.arr.length) {
this.wait();
}
b.putG();
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//篮子
class Basket {
public Goods[] arr = new Goods[6];
public int i = 0; //往篮子里面放包子
public synchronized void putG() throws InterruptedException {
Thread.sleep(1000);
arr[i] = new Goods();
System.out.println("做了一个包子----------现在篮子里面还有" + (i + 1) + "个包子");
i++;
} //从篮子里面拿包子
public synchronized void getG() throws InterruptedException {
Thread.sleep(1000);
arr[i] = null;
System.out.println("吃了一个包子----------现在篮子里面还有" + (i + 1) + "个包子");
i--;
}
}//包子
class Goods {
private int id; public Goods() {}; public Goods(int id) {
this.id = id;
}
}运行后 出这个。
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at test2.Customer.run(TestProductAndConsumer.java:27)
at java.lang.Thread.run(Unknown Source)
做了一个包子----------现在篮子里面还有1个包子
Basket b = new Basket();
new Thread(new Customer(b)).start();
new Thread(new Producer(b)).start();
}
}//生产者
class Customer implements Runnable {
private Basket b; public Customer() {
}; public Customer(Basket b) {
this.b = b;
} public void run() {
try {
while (true) {
if (b.i == 0) {
this.wait();
}
b.getG();
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//消费者
class Producer implements Runnable {
private Basket b; public Producer() {
}; public Producer(Basket b) {
this.b = b;
} public void run() {
try {
while (true) {
if (b.i == b.arr.length) {
this.wait();
}
b.putG();
this.notifyAll();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}//篮子
class Basket {
public Goods[] arr = new Goods[6];
public int i = 0; //往篮子里面放包子
public synchronized void putG() throws InterruptedException {
Thread.sleep(1000);
arr[i] = new Goods();
System.out.println("做了一个包子----------现在篮子里面还有" + (i + 1) + "个包子");
i++;
} //从篮子里面拿包子
public synchronized void getG() throws InterruptedException {
Thread.sleep(1000);
arr[i] = null;
System.out.println("吃了一个包子----------现在篮子里面还有" + (i + 1) + "个包子");
i--;
}
}//包子
class Goods {
private int id; public Goods() {}; public Goods(int id) {
this.id = id;
}
}运行后 出这个。
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at test2.Customer.run(TestProductAndConsumer.java:27)
at java.lang.Thread.run(Unknown Source)
做了一个包子----------现在篮子里面还有1个包子
解决方案 »
- struts1.2表单无法提交
- 项目在tomcat上是好的,移到glassfish上后有中文乱码
- MyEclipse里面注释JavaScript的快捷键是什么?
- JAVA群100952164,企业交流群
- 各位大侠,那个有jsp网页在线编译的代码,给我发一下!谢谢!
- jsp里面既有tag,又有el expression,还有java scriptlet,编译jsp的时候是怎么处理的循序的???
- Struts+Spring+Hibernate+JTA配置问题.table or view does not exist
- tomcat5.0做服务器,最多可以支持多少使用用户数?
- validator.rules.xml
- java转换复杂html为word
- 在webspere上运行出现java.lang.ArrayStoreException
- 求jsp大牛帮我解答一下
应该是你的Basket b = new Basket();
new Thread(new Customer(b)).start();
这里传递的对象b->当某个线程试图等待一个自己并不拥有的对象
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class ConditionTest { public static class Basket {
Lock lock = new ReentrantLock();// 锁
// 根据锁产生Condition对象
Condition produced = lock.newCondition();
Condition consumed = lock.newCondition();
int num = 0;// 篮子中的苹果数,最多为1 /** 生产苹果,往篮子里面放 */
public void produce() throws InterruptedException {
lock.lock();// 获得锁
System.out.println("Producer get a lock......");
try {
// 判断是否满足生产条件
while (num == 1) {
// 如果有苹果,则不生产,放弃锁,进入睡眠
// 等待消费者消费
System.out.println("Producer sleep......");
consumed.await();
System.out.println("Producer awaked.....");
}
// 生产苹果
Thread.sleep(500);
System.out.println("Producer produced an Apple.");
num = 1;
// 通知等待produced Condition的线程
produced.signal();
} finally {
lock.unlock();
}
} public void consume() throws InterruptedException {
lock.lock();
System.out.println("Consumer get a lock.......");
try {
// 判断是否满足消费条件
// 等待生产者生产苹果
while (num == 0) {
System.out.println("Consumer sleep.........");
produced.await();
System.out.println("Consumer awaked........");
}
// 吃苹果
Thread.sleep(500);
System.out.println("Consumer consumed an Apple.");
num = 0;
// 发信号唤醒某个等待consumed Condition的线程
consumed.signal();
} finally {
lock.unlock();
}
}
} /** 测试Basket程序 */
public static void testBasket() throws Exception {
final Basket basket = new Basket();
// 定义一个producer
Runnable producer = new Runnable() {
public void run() {
try {
basket.produce();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
// 定义一个consumer
Runnable consumer = new Runnable() {
public void run() {
try {
basket.consume();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
// 各生产3各consumer和producer
ExecutorService service = Executors.newCachedThreadPool();
for (int i = 0; i < 3; i++) {
service.submit(producer);
}
for (int i = 0; i < 3; i++) {
service.submit(consumer);
}
service.shutdown();
} public static void main(String args[]) throws Exception {
ConditionTest.testBasket();
}
}