关于Java的生产者消费者问题 有人能提供一下简单的Java生产者消费者模型代码吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public class TestThread { public static void main(String[] args) { Pool pool = new Pool(); Thread t1 = new ThreadIn(pool); Thread t2 = new ThreadDe(pool); t2.start(); t1.start(); }}class ThreadIn extends Thread { private Pool pool; public ThreadIn(Pool pool) { this.pool = pool; } public void run() { try { for (int i = 0; i < 10; i++) { pool.increase(); } } catch (Exception e) { e.printStackTrace(); } }}class ThreadDe extends Thread { private Pool pool; public ThreadDe(Pool pool) { this.pool = pool; } public void run() { try { for (int i = 0; i < 10; i++) { pool.decrease(); } } catch (Exception e) { e.printStackTrace(); } }}class Pool { private int num = 0; public synchronized void increase() throws Exception { while (num > 0) { this.wait(); } num++; System.out.println("num:" + num); notify(); } public synchronized void decrease() throws Exception { while (0 == num) { this.wait(); } num--; System.out.println("num:" + num); notify(); }} 请教exec调用cmd的问题 帮忙看一下这个java程序怎么会报错呢? 简单的问题,如何给1个vector对象中vector对象的元素赋值,要切实真确的,通过编译的,不要给伪代码 跪求sybase或oracle数据库中怎么才能实时获得某个表新插入的数据 Swing中,在点击一个按钮或者菜单时,如何判断Shift键被按下 几个简单的问题 add()为什么不用任何对象调用啊? Mingw编写程式问题 对象怎么序列化和反序列化? Linux下可以用 JNI或 JNA调用 dll吗? 自定义ClassLoader 与Webservice 的问题 这个表格如何用For循环或者别的办法实现
Pool pool = new Pool();
Thread t1 = new ThreadIn(pool);
Thread t2 = new ThreadDe(pool);
t2.start();
t1.start(); }}class ThreadIn extends Thread {
private Pool pool; public ThreadIn(Pool pool) {
this.pool = pool;
} public void run() {
try {
for (int i = 0; i < 10; i++) {
pool.increase();
} } catch (Exception e) { e.printStackTrace();
}
}
}class ThreadDe extends Thread {
private Pool pool; public ThreadDe(Pool pool) {
this.pool = pool;
} public void run() {
try {
for (int i = 0; i < 10; i++) {
pool.decrease();
} } catch (Exception e) { e.printStackTrace();
} }
}class Pool {
private int num = 0; public synchronized void increase() throws Exception { while (num > 0) {
this.wait();
}
num++;
System.out.println("num:" + num);
notify();
} public synchronized void decrease() throws Exception { while (0 == num) {
this.wait();
}
num--;
System.out.println("num:" + num);
notify();
}
}