java的Thread问题,我在线等答案
来源:百度知道 编辑:UC知道 时间:2024/05/11 04:05:19
class Producer implements Runnable
{
Q q;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
q.put("zhangkai","male");
else
q.put("wanghui","female");
i=(i+1)%2;
}
}
}
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
q.get();
}
}
class Q
{
boolean bfull=false;
String name="unknown";
String sex="unknown ";
public synchronized void put(String name,String sex)
{if(bfull)
try{wait();}catch(Exception e){}
this.name=name;
try {Thread.sleep(1);} catch(Exception e){}
this.sex=sex;
{
Q q;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
if(i==0)
q.put("zhangkai","male");
else
q.put("wanghui","female");
i=(i+1)%2;
}
}
}
class Consumer implements Runnable
{
Q q;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while(true)
q.get();
}
}
class Q
{
boolean bfull=false;
String name="unknown";
String sex="unknown ";
public synchronized void put(String name,String sex)
{if(bfull)
try{wait();}catch(Exception e){}
this.name=name;
try {Thread.sleep(1);} catch(Exception e){}
this.sex=sex;
很简单啊
你这个方法的最下面
public synchronized void get() {
if (!bfull)
try {
wait();
} catch (Exception e) {
}
System.out.print(name);
System.out.println(":" + sex);
bfull = false;
notify();
System.out.print(name);
System.out.println(":" + sex);
}
你看 你用了 notify(); 后又打印了一遍所以是两遍啊
去掉就是交替打印了
notify();
System.out.print(name);
System.out.println(":" + sex);
我运行之后的结果是
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
zhangkai:male
。。。
一直是这个
java的Thread问题,我在线等高手的指教
java的Thread问题,我在线等答案
java多线程的问题?-函数Thread.sleep(3);
关于java的问题 exception in thread "main".....
java Thread 里面的变量能不能访问?
java 的问题:这是用thread方法编程的,怎么用Runnable方法来继承??真的很想知道!!
java中thread的start()和run()有何区别?
运行java 中出现 except in thread "main" java.lang.noclassdeffounderror:o 是什么问题?
java 编译问题出错,提示Exception in thread "main" java.lang.NoSuchMethodError: main
Exception in thread "main" java.lang.Noclass DefFoundError问题