黑马程序员技术交流社区
标题:
多线程问题-运行结果怎么不对,不是我要的间隔输出。
[打印本页]
作者:
张丽
时间:
2012-10-6 19:57
标题:
多线程问题-运行结果怎么不对,不是我要的间隔输出。
为什么该程序输出的值不是间隔着输出一个人名字、性别和另一个人的名字和性别?总是有重复的。
package zhangli1;
public class ResourceDemo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread t1=new Thread(in);
Thread t2=new Thread(out);
t1.start();
t2.start();
}
}
class Resource{
String name;
String sex;
static boolean flag=false;
public synchronized void set(String name,String sex){
if(flag)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.name=name;
this.sex=sex;
flag=true;
this.notify();
}
public synchronized void out(String name,String sex){
if(!flag)
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(name+".........."+sex);
flag=false;
this.notify();
}
}
class Input implements Runnable{
Resource r;
Input(Resource r)
{
this.r=r;
}
public void run(){
int x=0;
while(true)
{
if(x==0)
{
r.set("美丽", "女女女女女女女女");
}
else
r.set("JOHN","NAN");
x=(x+1)%2;
}
}
}
class Output implements Runnable{
Resource r;
Output(Resource r){
this.r=r;
}
public void run(){
while(true){
r.out(r.name, r.sex);
}
}
}
输出结果是这样的:
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
美丽..........女女女女女女女女
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
美丽..........女女女女女女女女
美丽..........女女女女女女女女
JOHN..........NAN
美丽..........女女女女女女女女
作者:
古银平
时间:
2012-10-6 20:28
class Res
{
private String name;
private String sex;
private boolean flag = false;
public synchronized void set(String name,String sex)
{
if(flag)
try{this.wait();}catch(Exception e){}
this.name = name;
this.sex = sex;
flag = true;
this.notify();
}
public synchronized void out()
{
if(!flag)
try{this.wait();}catch(Exception e){}
System.out.println(name+"........"+sex);
flag = false;
this.notify();
}
}
class Input implements Runnable
{
private Res r ;
Input(Res r)
{
this.r = r;
}
public void run()
{
int x = 0;
while(true)
{
if(x==0)
r.set("mike","man");
else
r.set("丽丽","女女女女女");
x = (x+1)%2;
}
}
}
class Output implements Runnable
{
private Res r ;
Output(Res r)
{
this.r = r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
class InputOutputDemo2
{
public static void main(String[] args)
{
Res r = new Res();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
/*
Input in = new Input(r);
Output out = new Output(r);
Thread t1 = new Thread(in);
Thread t2 = new Thread(out);
t1.start();
t2.start();
*/
}
}
复制代码
对照这个看看吧
作者:
叶征东
时间:
2012-10-7 11:49
本帖最后由 叶征东 于 2012-10-7 12:06 编辑
....................
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2