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连不上sqlserver2005 public class 区别的问题 求解 为什么老会出现内存溢出(急) 使用JTABLE为什么没有表头? html页面 document.getelementbyid 和form.id的区别,很纳闷的问题. JDesktopPane中还能不能再除了JInternalFrame处的别的GUI组件呢?????? InstallShield可以安装JSP应用程序吗? java调用数据库 那位哥们能举个通过jndi连接连接池的例子??多谢!!! 高分求助:Jbuilder6与Weblogic6.1的整合 请问如何创建目录? 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);
}
}