java中的多线程问题 thinking in java里有的讲 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是一个线程同步问题,你好好体会一下吧!class q {//产生同步序列 int n; boolean val=false; synchronized int get(){ if(!val){ try{ wait(); }catch(java.lang.InterruptedException e){} } System.out.println("get: "+n); val=false; notify(); return n; } synchronized int put(int n){ if(val){ try{ wait(); }catch(java.lang.InterruptedException e){} } this.n=n; val=true; System.out.println("put:"+n); notify(); return n; }}class pro implements Runnable{//生产者 q q1; pro(q q1){ this.q1=q1; new Thread(this).start(); } public void run(){ int i=0; while(i!=5) try{ q1.put(i++); Thread.sleep(1000); }catch(java.lang.InterruptedException e){} }}class cust implements Runnable{//消费者 q q1; cust(q q1){ this.q1=q1; new Thread(this).start(); } public void run(){ while(true){ q1.get(); } } public static void main(String[] args) { q q1=new q(); new pro(q1); new cust(q1); }} ★Java版本冲突问题? 用JMF拍照并在本地硬盘中保存遇到的问题 看看下段程序错到那了 (很短) 谢谢 如何用java编写flash播放器(或者调用flashplayer6installer.exe,它是不能自动搜寻文件的) 问书 java core 求教:java Applet中如何调用java script java初学者请教命令行编译的问题 大家帮我看看这个怎么实现呀, 哪们有关于JNI的电子书或网上资料,能否给我一份 写了一段小代码输出验证码图片,出现了问题。请大神指点 请问如何创建目录? JLabel的问题!
int n;
boolean val=false;
synchronized int get(){
if(!val){
try{
wait();
}catch(java.lang.InterruptedException e){}
} System.out.println("get: "+n);
val=false;
notify();
return n;
} synchronized int put(int n){
if(val){
try{
wait();
}catch(java.lang.InterruptedException e){}
}
this.n=n;
val=true;
System.out.println("put:"+n); notify();
return n;
}
}class pro implements Runnable{//生产者
q q1; pro(q q1){
this.q1=q1;
new Thread(this).start();
} public void run(){
int i=0;
while(i!=5)
try{
q1.put(i++);
Thread.sleep(1000);
}catch(java.lang.InterruptedException e){}
}
}
class cust implements Runnable{//消费者
q q1;
cust(q q1){
this.q1=q1;
new Thread(this).start();
} public void run(){
while(true){
q1.get();
}
}
public static void main(String[] args) {
q q1=new q();
new pro(q1);
new cust(q1);
}
}