class shen implements Runnable
{
xuni xu;
public shen(xuni xu)
{
this.xu=xu;
}
public void run()
{
int x=0;
while(true)
{
synchronized(xu)
{
try
{
if(xu.bfull)
{
xu.wait();
}
}
catch(Exception ex)
{
}
if(x==0)
{
xu.name="lizhao";
//try{Thread.sleep(1);}catch(Exception ex){}
xu.sex="man";
}
else
{
xu.name="xingyu";
xu.sex="male";
}
xu.bfull=true;
xu.notify();
}
x=(x+1)%2;
}
}
}
class xiao implements Runnable
{
xuni xu;
public xiao(xuni xu)
{
this.xu=xu;
}
public void run()
{
while(true)
{
synchronized(xu)
{
try
{
if(!xu.bfull)
{
xu.wait();
}
}
catch(Exception ex)
{
}
System.out.print(xu.name+":");
System.out.println(xu.sex);
xu.bfull=false;
xu.notify();
}
}
}
}
class xuni
{
String name="no";
String sex="no";
boolean bfull=false; //缓冲区中不为空的标识
}
class zhu
{
public static void main(String[] args)
{
xuni xu2=new xuni();
new Thread(new shen(xu2)).start();
new Thread(new xiao(xu2)).start();
}
}以上程序的运行顺序是怎么样的?是先运行xiao类中的run方法,还是先运行shen类中的run方法.
bfull=false代表缓冲区不为空.如果先运行xiao类中的run方法,就直接进入等待状态了.如果是先运行shen类中的run方法,也是进入等待状态了.我对这个迷糊的不行,谁能给我解读一下:)
{
xuni xu;
public shen(xuni xu)
{
this.xu=xu;
}
public void run()
{
int x=0;
while(true)
{
synchronized(xu)
{
try
{
if(xu.bfull)
{
xu.wait();
}
}
catch(Exception ex)
{
}
if(x==0)
{
xu.name="lizhao";
//try{Thread.sleep(1);}catch(Exception ex){}
xu.sex="man";
}
else
{
xu.name="xingyu";
xu.sex="male";
}
xu.bfull=true;
xu.notify();
}
x=(x+1)%2;
}
}
}
class xiao implements Runnable
{
xuni xu;
public xiao(xuni xu)
{
this.xu=xu;
}
public void run()
{
while(true)
{
synchronized(xu)
{
try
{
if(!xu.bfull)
{
xu.wait();
}
}
catch(Exception ex)
{
}
System.out.print(xu.name+":");
System.out.println(xu.sex);
xu.bfull=false;
xu.notify();
}
}
}
}
class xuni
{
String name="no";
String sex="no";
boolean bfull=false; //缓冲区中不为空的标识
}
class zhu
{
public static void main(String[] args)
{
xuni xu2=new xuni();
new Thread(new shen(xu2)).start();
new Thread(new xiao(xu2)).start();
}
}以上程序的运行顺序是怎么样的?是先运行xiao类中的run方法,还是先运行shen类中的run方法.
bfull=false代表缓冲区不为空.如果先运行xiao类中的run方法,就直接进入等待状态了.如果是先运行shen类中的run方法,也是进入等待状态了.我对这个迷糊的不行,谁能给我解读一下:)
1、将代码进行良好的格式化,以方便阅读。
2、在发帖文本框的上方单击“#”按钮,选择 Java
3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello { // 程序入口
public static void main(String[] args) {
System.out.println("Hello!");
}
}
public class readPro
{
public static void main(String[] args) throws IOException
{
Properties pro = new Properties();
FileInputStream fis = new FileInputStream("pro.properties");
pro.load(fis);
}}
自已参考这:要想调用wait()就得先获得对象锁(所以wait()只能在同步语句块里调用),如果一个线程 wait()它就会在释放对象锁
的同时进入等待池,除非有线程触发notify()才能把它从等待池中释放出来
两种情况:
运行状态--(synchronized)----->得到对象锁--------(wait())------->等待池---(外部的notify())---->synchronized外等待等待
得不到就一直等待(在对象锁池) 没notify()就一直在等待池里等着
(除非它再次获得对象锁)
运行状态--synchronized------->进入对象锁池----得到对象锁--->就绪状态
class test1 implements Runnable
{
int able=0;
test3 t3;
public test1(test3 t3)
{
this.t3=t3;
}
public void run()
{
while(true)
{
if(able==0)
{
t3.set("lizhao","man");
able=1;
}
else
{
t3.set("xingyu","male ");
able=0;
}
}
}
}
class test2 implements Runnable
{
test3 t3;
public test2(test3 t3)
{
this.t3=t3;
}
public void run()
{
while(true)
{
t3.get();
}
}
}
class test3
{
String name="no";
String sex="no";
boolean bfull=false;
public synchronized void set(String name,String sex)
{
if(bfull)
{
try
{
wait();
}
catch(Exception ex)
{
}
}
this.name=name;
this.sex=sex;
bfull=true;
notify();
}
public synchronized void get()
{
if(!bfull)
{
System.out.println(!bfull);
try
{
wait();
}
catch(Exception ex)
{
}
}
System.out.println(name+":"+sex);
bfull=false;
notify();
}
}
class test4
{
public static void main(String[] args)
{
test3 t3=new test3();
new Thread(new test1(t3)).start();
new Thread(new test2(t3)).start();
}
}
我把原来的代码改动了一下,这样看起来清楚点.
问题是:
在 test3类中的set()方法中,if(bfull)这个括号中的bfull是true还是false ?如果是true,是不是一开始就进入了wait状态?bfull的初始化是false,怎么会变成true呢?
wait()在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待。
它们都是object中的方法.
* 1>如果是true,是不是一开始就进入了wait状态?是的
* 2>bfull的初始化是false,怎么会变成true呢?因为它被test3.set()方法给了
* 这道题: 你只能先set(),而不能先get(),既使你先get(),它也会被wait(),只到set()完去notify它
* 去看看"生产着和消费着",它里面就把wait()和notify()表达的很清楚了
*
*
*/