A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 官仁杰 于 2012-10-19 12:41 编辑

相当于复写毕老师视频的代码,写出来结果不正确
  1. public class InOutDemo {

  2.         public static void main(String[] args) {
  3.                 Person p = new Person();
  4.                 new Thread(new DataIn(p)).start();
  5.                 new Thread(new DataOut(p)).start();
  6.         }
  7. }

  8. class Person{
  9.         String name;
  10.         int age;
  11.         boolean flag = true;
  12. }

  13. class DataIn implements Runnable{
  14.         private Person p;
  15.         DataIn(Person p){
  16.                 this.p = p;
  17.         }
  18.         @Override
  19.         public void run() {
  20.                 int x = 0;
  21.                 while(true){
  22.                         synchronized(p){
  23.                                 if(!p.flag){
  24.                                         try {
  25.                                                 p.wait();
  26.                                         } catch (InterruptedException e) {
  27.                                                 e.printStackTrace();
  28.                                         }
  29.                                 }
  30.                                 if(x == 0){
  31.                                         p.name = "Linda";
  32.                                         p.age = 10;
  33.                                 }
  34.                                 else{
  35.                                         p.name = "Peter";
  36.                                         p.age =20;
  37.                                 }
  38.                                 x = x == 0? 1:0;         
  39.                                 p.flag = false;
  40.                                 p.notify();
  41.                         }        
  42.                 }        
  43.         }        
  44. }

  45. class DataOut implements Runnable{
  46.         private Person p;
  47.         DataOut(Person p){
  48.                 this.p = p;
  49.         }
  50.         @Override
  51.         public void run() {
  52.                 while(true){
  53.                         synchronized(p){
  54.                                 if(p.flag){
  55.                                         try {
  56.                                                 p.wait();
  57.                                         } catch (InterruptedException e) {
  58.                                                 e.printStackTrace();
  59.                                         }
  60.                                 }
  61.                                 System.out.println(p.name +"的年龄是" +p.age);
  62.                         }
  63.                         p.flag = true;
  64.                         p.notify();
  65.                 }
  66.         }
  67. }
复制代码
输出Exception in thread "Thread-1" java.lang.IllegalMonitorStateException        at java.lang.Object.notify(Native Method)
        at DataOut.run(InOutDemo.java:69)
        at java.lang.Thread.run(Unknown Source)
Linda的年龄是10

评分

参与人数 1技术分 +1 收起 理由
韩军博 + 1 很给力!

查看全部评分

2 个回复

倒序浏览
class TestNo
{
public static void main(String[] args)
{
  Person p=new Person();
    new Thread(new InPut(p)).start();
    new Thread(new OutPut(p)).start();
}
}
class Person
{
String name;
int age;
boolean flag=false;
}
class InPut implements Runnable
{
private Person p;
InPut(Person p)
{
  this.p=p;
}
public void run()
{
  int x=0;
  while(true)
  {
   synchronized(p)
   {
    if(p.flag)
   
     try
     {
      p.wait();
     }
     catch(InterruptedException  e)
     {
      e.printStackTrace();

     }
    if(x==0)
    {
     p.name="mike";
     p.age=1;
    }
    else
    {
     p.name="张三";
     p.age=20;
    }
    p.flag=true;
    x = x == 0? 1:0;
    p.notify();
   
   }
  }
}
}
class OutPut implements Runnable
{
private Person p;
OutPut(Person p)
{
  this.p=p;
}
public void run()
{
  while(true)
  {
   synchronized(p)
   {
    if(!p.flag)
    try
    {
     p.wait();
    }
    catch(InterruptedException  e)
    {
     e.printStackTrace();

    }
    System.out.println(p.name+"年龄"+p.age);
    p.flag=false;
    p.notify();
   }
  }
}
}
这样可以执行
回复 使用道具 举报
本帖最后由 徐升兴 于 2012-10-19 11:52 编辑

lz,你的问题有关一个括号。
**************

输出Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
提示信息表明,线程2的监视器非法,所以只看线程2的synchronized的代码片段。
发现出错,应该如下所示:
p.notify();//把进程唤醒放进synchronized的{}中。

最后运行结果为:
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马