notify(),waite,老调重谈. a Threadfor(;;){ a.send(); a.wait();}b Threadfor(;;){ b.rec(); b.notifyall();} 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package concurrent;public class Consumer implements Runnable { private Inventory inv; public Consumer(Inventory inv) { if (inv == null) throw new NullPointerException(); this.inv = inv; } public void run() { while (true) { try { Object item = Consumer.this.inv.get(); Thread.sleep( (int) (1000 * Math.random())); } catch (InterruptedException ie) { ie.printStackTrace(); } } }}----------------------------------------------------------------------package concurrent;public class Producer implements Runnable { private Inventory inv; public Producer(Inventory inv) { this.inv = inv; } public void run() { while (true) { Object item = new Integer( (int) (100 * Math.random())); try { this.inv.put(item); Thread.sleep( (int) (5 * 1000 * Math.random())); } catch (InterruptedException ie) { ie.printStackTrace(); } } }}----------------------------------------------------------------------package concurrent;import java.util.*;public class Inventory { private final int size; private List items = null; public Inventory(int size) { this.size = size; this.items = new ArrayList(size); } public int getSize() { return this.size; } public synchronized int getItemCount() { return this.items.size(); } public synchronized List getItems() { return new ArrayList(this.items); } public synchronized String toString() { return "Inventory:" + items.toString(); } public Object get() throws InterruptedException { if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } synchronized (this) { try { while (items.size() == 0) { wait(); } int tsize = this.items.size(); Object item = this.items.remove(tsize - 1); System.out.println( "---------------------------------------------"); System.out.println(Thread.currentThread() + " CONSUME " + item); System.out.println(this.items.toString()); notify(); return item; } catch (InterruptedException ie) { notify(); throw ie; } } } public void put(Object item) throws InterruptedException { if (item == null) throw new NullPointerException(); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } synchronized (this) { try { while (this.items.size() == this.size) { wait(); } this.items.add(item); System.out.println( "---------------------------------------------"); System.out.println(Thread.currentThread() + " PRODUCE " + item); System.out.println(this.items.toString()); notify(); } catch (InterruptedException ie) { notify(); throw ie; } } }}----------------------------------------------------------------------package concurrent;public class Main { public static void main(String[] args) { Inventory inv = new Inventory(10); Consumer c1 = new Consumer(inv); //Consumer c2 = new Consumer(inv); Producer p1 = new Producer(inv); Producer p2 = new Producer(inv); Producer p3 = new Producer(inv); new Thread(c1).start(); //new Thread(c2).start(); new Thread(p1).start(); new Thread(p2).start(); new Thread(p3).start(); }} 上面的程序是错的,至少只适合一个消费者和一个生产者,羞ing... Inventory.java改成这样应该可以了吧,版我瞧瞧。---------------------------------------------------------------------package concurrent;import java.util.*;public class Inventory { private final int size; private List items = null; private Object consumerLock = new Object(); private Object producerLock = new Object(); private int itemCount; private int spaceCount; public Inventory(int size) { if(size<=0) throw new IllegalArgumentException(); this.size = size; this.items = new ArrayList(size); this.itemCount = 0; this.spaceCount = size; } public int getSize() { return this.size; } public synchronized int getItemCount() { return this.items.size(); } public synchronized List getItems() { return new ArrayList(this.items); } public synchronized String toString() { return "Inventory:" + items.toString(); } //-------------------------------------------------------------------------- public Object get() throws InterruptedException { if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } Object item = null; synchronized (this.consumerLock) { try { while (this.itemCount == 0) { this.consumerLock.wait(); } } catch (InterruptedException ie) { this.consumerLock.notify(); throw ie; } synchronized (this) { int tsize = this.items.size(); item = this.items.remove(tsize - 1); System.out.println( "---------------------------------------------"); System.out.println(Thread.currentThread() + " CONSUME " + item); System.out.println(this.items.toString()); } this.itemCount--; } synchronized (this.producerLock) { this.spaceCount++; this.producerLock.notify(); } return item; } public void put(Object item) throws InterruptedException { if (item == null) throw new NullPointerException(); if (Thread.currentThread().isInterrupted()) { throw new InterruptedException(); } synchronized (this.producerLock) { try { while (this.spaceCount == 0) { this.producerLock.wait(); } } catch (InterruptedException ie) { this.producerLock.notify(); throw ie; } synchronized (this) { this.items.add(item); System.out.println( "---------------------------------------------"); System.out.println(Thread.currentThread() + " CONSUME " + item); System.out.println(this.items.toString()); } this.spaceCount--; } synchronized (this.consumerLock) { this.itemCount++; this.consumerLock.notify(); } }} synchronized 问题 java图像处理 多线程的一点疑惑(高手入) 如何播放比特数组里的数据(类似于流媒体的思想) 求用户权限管理的相关实例或模型 求助:用java往文件里写入数据 菜鸟问题,请解答 100分!!!!!!求用log4j产生xml日志文件的原代码, 不够再加... 请大家帮忙做一道题!!十分着急!! entitybean调用错误 各位高手!帮个忙好吗?这是个很菜的问题! 怎么在java里给数据库插入一条新记录然后立刻取出此条记录的id(sqlserver).而不会取到几乎同时插入的别人的记录id
implements Runnable {
private Inventory inv;
public Consumer(Inventory inv) {
if (inv == null)
throw new NullPointerException(); this.inv = inv;
} public void run() {
while (true) {
try {
Object item = Consumer.this.inv.get();
Thread.sleep( (int) (1000 * Math.random()));
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
----------------------------------------------------------------------
package concurrent;public class Producer
implements Runnable {
private Inventory inv;
public Producer(Inventory inv) {
this.inv = inv;
} public void run() {
while (true) {
Object item = new Integer( (int) (100 * Math.random()));
try {
this.inv.put(item);
Thread.sleep( (int) (5 * 1000 * Math.random()));
}
catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
----------------------------------------------------------------------
package concurrent;import java.util.*;public class Inventory {
private final int size;
private List items = null; public Inventory(int size) {
this.size = size;
this.items = new ArrayList(size);
} public int getSize() {
return this.size;
} public synchronized int getItemCount() {
return this.items.size();
} public synchronized List getItems() {
return new ArrayList(this.items);
} public synchronized String toString() {
return "Inventory:" + items.toString();
} public Object get() throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
} synchronized (this) {
try {
while (items.size() == 0) {
wait();
} int tsize = this.items.size();
Object item = this.items.remove(tsize - 1); System.out.println(
"---------------------------------------------");
System.out.println(Thread.currentThread() + " CONSUME " + item);
System.out.println(this.items.toString()); notify();
return item;
}
catch (InterruptedException ie) {
notify();
throw ie;
}
}
} public void put(Object item) throws InterruptedException {
if (item == null)
throw new NullPointerException(); if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
} synchronized (this) {
try {
while (this.items.size() == this.size) {
wait();
} this.items.add(item); System.out.println(
"---------------------------------------------");
System.out.println(Thread.currentThread() + " PRODUCE " + item);
System.out.println(this.items.toString()); notify();
}
catch (InterruptedException ie) {
notify();
throw ie;
}
}
}
}
----------------------------------------------------------------------
package concurrent;public class Main {
public static void main(String[] args) {
Inventory inv = new Inventory(10); Consumer c1 = new Consumer(inv);
//Consumer c2 = new Consumer(inv); Producer p1 = new Producer(inv);
Producer p2 = new Producer(inv);
Producer p3 = new Producer(inv); new Thread(c1).start();
//new Thread(c2).start(); new Thread(p1).start();
new Thread(p2).start();
new Thread(p3).start();
}
}
---------------------------------------------------------------------
package concurrent;import java.util.*;public class Inventory {
private final int size;
private List items = null; private Object consumerLock = new Object();
private Object producerLock = new Object(); private int itemCount;
private int spaceCount; public Inventory(int size) {
if(size<=0) throw new IllegalArgumentException(); this.size = size;
this.items = new ArrayList(size); this.itemCount = 0;
this.spaceCount = size;
} public int getSize() {
return this.size;
} public synchronized int getItemCount() {
return this.items.size();
} public synchronized List getItems() {
return new ArrayList(this.items);
} public synchronized String toString() {
return "Inventory:" + items.toString();
} //--------------------------------------------------------------------------
public Object get() throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
} Object item = null;
synchronized (this.consumerLock) {
try {
while (this.itemCount == 0) {
this.consumerLock.wait();
}
}
catch (InterruptedException ie) {
this.consumerLock.notify();
throw ie;
} synchronized (this) {
int tsize = this.items.size();
item = this.items.remove(tsize - 1); System.out.println(
"---------------------------------------------");
System.out.println(Thread.currentThread() +
" CONSUME " + item);
System.out.println(this.items.toString());
} this.itemCount--;
} synchronized (this.producerLock) {
this.spaceCount++;
this.producerLock.notify();
}
return item;
} public void put(Object item) throws InterruptedException {
if (item == null)
throw new NullPointerException(); if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
} synchronized (this.producerLock) {
try {
while (this.spaceCount == 0) {
this.producerLock.wait();
}
}
catch (InterruptedException ie) {
this.producerLock.notify();
throw ie;
} synchronized (this) {
this.items.add(item); System.out.println(
"---------------------------------------------");
System.out.println(Thread.currentThread() +
" CONSUME " + item);
System.out.println(this.items.toString());
} this.spaceCount--;
} synchronized (this.consumerLock) {
this.itemCount++;
this.consumerLock.notify();
}
}
}