a Thread
for(;;){
a.send();
a.wait();
}
b Thread
for(;;){
b.rec();
b.notifyall();
}
for(;;){
a.send();
a.wait();
}
b Thread
for(;;){
b.rec();
b.notifyall();
}
解决方案 »
- NetBeans jTable绑定的刷新问题
- 急救jdk中的JAR文件打不开
- 高手帮我分析一下下面JAVA程序的值传递,谢谢!我是新手.
- 关于表数据刷新,增加一列之后,jtable
- 菜单的问题,在线急等
- 请高手指教啊!class$还有关于exception,谢谢!
- java中如何传递鼠标消息啊?从子panel传递鼠标点击到父panel
- 在linux下有没有小巧的java编辑器?
- JTEXTAREA???
- tomcat报错,求大家给看看什么原因
- 各位高手!帮个忙好吗?这是个很菜的问题!
- 怎么在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();
}
}
}