class Q
{
private String name="张三";
private String sex="男";
public synchronized void put(String name,String sex)
{
this.name=name;
/*try
{
Thread.sleep(10);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}*/
this.sex=sex;
}
public synchronized void get()
{
System.out.println(name+"------>"+sex);
}
}clsss Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while (true)
{
if (i==0)
{
q.put("张三","男");
}
else
{
q.put("李四","女");
}
}
}
}
class Consumer implements Runnable
{
Q q=null;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while (true)
{
q.get();
}
}
}
public class ThreadCommunication
{
public static void main(String[] args)
{
Q q=new Q();
new Thread((new Producer()).q).start();
new Thread((new consumer()).q).start();
}
}
以上语句为什么执行不成功,谢谢!!!
{
private String name="张三";
private String sex="男";
public synchronized void put(String name,String sex)
{
this.name=name;
/*try
{
Thread.sleep(10);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}*/
this.sex=sex;
}
public synchronized void get()
{
System.out.println(name+"------>"+sex);
}
}clsss Producer implements Runnable
{
Q q=null;
public Producer(Q q)
{
this.q=q;
}
public void run()
{
int i=0;
while (true)
{
if (i==0)
{
q.put("张三","男");
}
else
{
q.put("李四","女");
}
}
}
}
class Consumer implements Runnable
{
Q q=null;
public Consumer(Q q)
{
this.q=q;
}
public void run()
{
while (true)
{
q.get();
}
}
}
public class ThreadCommunication
{
public static void main(String[] args)
{
Q q=new Q();
new Thread((new Producer()).q).start();
new Thread((new consumer()).q).start();
}
}
以上语句为什么执行不成功,谢谢!!!
解决方案 »
- 如何在JTextField里显示一些字符,当鼠标点击JTextField区域后这些字符清空?
- 关于"云计算",你有什么看法
- 小第弱弱的问下 :如何在JcomboBox的右边加上Buttton
- 怎么把list里面的图片读出来?poi
- 一个汽车销售管理系统主要有什么模块,大大们进
- Java 的JDBC 数据库连接池实现方法
- java与mfc的通讯问题!
- 怎样用java写unix上的daemon程序?
- 用JBUILDER做出的applet好象不能用
- 求高手点拨一个Java Socket通信编程中的java.net.SocketException: Connection reset错误的调试方法。
- 刚学java,能给点提示吗?
- 怎么看JAVA文档
{
Q q=new Q();
new Thread((new Producer(q))).start();
new Thread((new Consumer(q))).start();
}
ThreadCommunication.java:24:需要为class、interface或enum class Producer implements Runnable.......后面还有很多行,好像都是同样的错误。