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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 于启会 中级黑马   /  2012-9-5 15:13  /  1595 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 于启会 于 2012-9-5 16:56 编辑
  1. <p>----Resource类:
  2. package com.Demo;</p><p>public class Resource {
  3. private String name;
  4. private String sex;
  5. private boolean flog=false;
  6. public void set(String name,String sex){
  7.   synchronized (this) {
  8.    if(flog){
  9.     try{
  10.      this.wait();
  11.      }catch (Exception e) {
  12.      }
  13.    }
  14.    this.name=name;
  15.    this.sex=sex;
  16.    flog=true;
  17.    this.notify();
  18.   }
  19. }

  20. public void get(){
  21.   synchronized (this) {
  22.    if(!flog){
  23.     try{
  24.      this.wait();
  25.     }catch (Exception e) {
  26.     }
  27.    }
  28.    System.out.println(name+"......"+sex);
  29.    flog=false;
  30.    this.notify();
  31.   }
  32. }
  33. }

  34. -----Input类:
  35. package com.Demo;</p><p>public class Input implements Runnable{
  36. private Resource r;
  37. public Input(Resource r){
  38.   this.r=r;
  39. }
  40. public void run() {
  41.   int x=0;
  42.   while(true){
  43.    if(x==0){
  44.     r.set("丽丽", "女");
  45.    }else{
  46.     r.set("龙龙", "男");
  47.    }
  48.    x=(x+1)%2;
  49.   }
  50. }
  51. }
  52. ----Output类:
  53. package com.Demo;</p><p>public class Output implements Runnable{
  54. private Resource r;
  55. public Output(Resource r){
  56.   this.r=r;
  57. }
  58. public void run() {
  59.   while(true){
  60.    r.get();
  61.   }
  62. }</p><p>}

  63. ---MainDemo类:
  64. package com.Demo;</p><p>public class MainDemo {
  65. public static void main(String[] args) {
  66.   new Thread(new Input(new Resource())).start();
  67.   new Thread(new Output(new Resource())).start();
  68. }
  69. }

  70. 打印结果是null....null闪一下然后不输出,求指导。。</p>
复制代码

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 以后多注意一下这点细节问题就对了,写程序.

查看全部评分

3 个回复

倒序浏览
  1. class Resouce
  2. {
  3.         private String name;
  4.         private String sex;
  5.         private boolean flag = true;
  6.         Resouce()
  7.         {
  8.         }
  9.         Resouce(String name,String sex)
  10.         {
  11.                 this.name = name;
  12.                 this.sex = sex;
  13.         }
  14.        
  15.         public synchronized void setRes(String name,String sex)
  16.         {
  17.                 if(!flag)
  18.                         try
  19.                         {
  20.                                 wait();
  21.                         }
  22.                         catch (InterruptedException ie)
  23.                         {
  24.                                 ie.printStackTrace();
  25.                         }
  26.                 this.name = name;
  27.                 this.sex = sex;

  28.                 flag = false;
  29.                 notify();
  30.         }
  31.        
  32.         public synchronized void getRes()
  33.         {
  34.                 if(flag)
  35.                         try
  36.                         {
  37.                                 wait();
  38.                         }
  39.                         catch (InterruptedException ie)
  40.                         {
  41.                                 ie.printStackTrace();
  42.                         }
  43.                 System.out.println(/*Thread.currentThread().getName()+*/"name:"+name+"..."+"sex:"+sex);
  44.                 flag = true;
  45.                 notify();
  46.         }
  47. }

  48. class  Input implements Runnable
  49. {
  50.         private  Resouce r;

  51.         Input(Resouce r)
  52.         {
  53.                 this.r = r;
  54.         }
  55.         public void run()
  56.         {
  57.                 int x = 0;
  58.                 while(true)
  59.                 {
  60.                         if(x==0)
  61.                                 r.setRes("丽丽","女");
  62.                         else
  63.                                 r.setRes("龙龙","男");
  64.                         x = (x+1)%2;
  65.                 }
  66.         }
  67. }
  68. class Output implements Runnable
  69. {
  70.         private Resouce r;
  71.         Output(Resouce r)
  72.         {
  73.                 this.r = r;
  74.         }
  75.         public void run()
  76.         {
  77.                 while(true)
  78.                 {
  79.                         r.getRes();
  80.                 }
  81.         }
  82. }

  83. class  InputOutputThread2
  84. {
  85.         public static void main(String[] args)
  86.         {
  87.                 Resouce r = new Resouce();
  88.                
  89.                 new Thread(new Input(r),"存储").start();
  90.                 new Thread(new Output(r),"读出").start();
  91.         }
  92. }
复制代码
看看这个结果行不行
回复 使用道具 举报
你定义了两个Resource类的对象,可以这样分析一下,
先分别给两个Resource类的对象命名一下,一个是Resource A ,一个是Resource B;
简称一下线程,一个是线程input,一个是线程output
input线程先启动,给A 设置进去了name和age的值,然后就wait等着output线程来取,
可是,output线程已启动却是等着取出B的值,
它们将一直等下去,因为不会有线程给B设置,也不会有线程去取A的值
所以,把main方法里的代码修改如下:
Resource res = new Resource();  //创建一个Resource对象让两个线程去操作,否则你定义的synchronized同步方法没有意义嘛!
new Thread(new Input(res)).start();
new Thread(new Output(res)).start();
另外,唤醒线程的方法,尽量不要用notify;要用notifyAll,因为线程都是线程调度器来调用的,你是控制不了的,所以用notify有时会出问题

评分

参与人数 1技术分 +1 收起 理由
刘芮铭 + 1 不错,分析的很到位

查看全部评分

回复 使用道具 举报
袁艳超 发表于 2012-9-5 16:37
你定义了两个Resource类的对象,可以这样分析一下,
先分别给两个Resource类的对象命名一下,一个是Resourc ...

额   创建对象那里写错了。。找了半天就是没找main方法中的代码。。受教了。。:hug:
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马