实际想输出zhangsan:fale
lisi:female
可是总是输出zhangsan:falei=(i+1)%2;这里应该怎么改呢?或者还有其它的吗?class Produce implements Runnable
{
Q q;
public Produce(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
synchronized(q)
{
if(i==0)
{
q.name="zhangsan";
try{Thread.sleep(1);}catch(Exception e){}
q.sex="fale";
}
else
{
q.name="lisi";
q.sex="female";
}
}
i = (i+1)%2;
}
}
}
class Customer implements Runnable
{
Q q;
public Customer(Q q)
{
this.q=q;
}
public void run()
{
{
while(true)
{
synchronized(q)
{
System.out.print(q.name);
System.out.println(":"+q.sex);
}
}
}
}
}
class Q
{
String name="unknow";
String sex="unknow";
}
class Test55
{
public static void main(String[] args)
{
Q q=new Q();
new Thread(new Produce(q)).start();
new Thread(new Customer(q)).start();
}
}
lisi:female
可是总是输出zhangsan:falei=(i+1)%2;这里应该怎么改呢?或者还有其它的吗?class Produce implements Runnable
{
Q q;
public Produce(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while(true)
{
synchronized(q)
{
if(i==0)
{
q.name="zhangsan";
try{Thread.sleep(1);}catch(Exception e){}
q.sex="fale";
}
else
{
q.name="lisi";
q.sex="female";
}
}
i = (i+1)%2;
}
}
}
class Customer implements Runnable
{
Q q;
public Customer(Q q)
{
this.q=q;
}
public void run()
{
{
while(true)
{
synchronized(q)
{
System.out.print(q.name);
System.out.println(":"+q.sex);
}
}
}
}
}
class Q
{
String name="unknow";
String sex="unknow";
}
class Test55
{
public static void main(String[] args)
{
Q q=new Q();
new Thread(new Produce(q)).start();
new Thread(new Customer(q)).start();
}
}
解决方案 »
- 怎样在JTable的单元格中加toolTip?
- 我是门外汉,求救关于catalina.sh的配置问题。
- 新手,求解答.
- 问一个菜问题 java程序能做成exe么 ?
- Socket的使用问题,只差一个错误就编译通过了!!!!!
- 字符串分割的怪问题.
- 使用swing组件中的jList时遇到问题,有用过jList的请进来一下
- 请教各位大侠如何在JTree树中得到各个节点显示出来的字串
- 在windowsXP上装上了jdk1.2.2也写了autoexec.bat,可是运行时总说没有这个命令,why?
- 小白请教问题(二)
- 单步调试的时候 定义变量的地方停不了是为什么啊
- 为什么不能改变JavaScript内置类Number.MAX_VALUE
结果前10条
zhangsan:fale
lisi:female
zhangsan:fale
lisi:female
zhangsan:fale
zhangsan:fale
lisi:female
zhangsan:fale
lisi:female
zhangsan:fale