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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 耿渊博 中级黑马   /  2014-3-25 00:50  /  1094 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 耿渊博 于 2014-3-25 23:16 编辑
  1. package com.Thread;
  2. class Res{
  3.         String name;
  4.         String sex;
  5.         boolean flag;
  6. }

  7. class Input implements Runnable{
  8.         Res r = new Res();
  9.         Input(Res r){
  10.                 this.r = r;
  11.         }
  12.         public void run(){
  13.                 int x = 0;
  14.                 while (true) {
  15.                         synchronized(r){
  16.                         if(r.flag)
  17.                                 try {
  18.                                         r.wait();
  19.                                 } catch (InterruptedException e) {
  20.                                         // TODO Auto-generated catch block
  21.                                         //e.printStackTrace();
  22.                                 }
  23.                         if (x == 0) {
  24.                                 r.name = "Mike";
  25.                                 r.sex = "man";
  26.                         } else {
  27.                                 r.name = "丽丽";
  28.                                 r.sex = "女女女女女";
  29.                         }
  30.                         x = (x + 1) % 2;
  31.                         r.flag = true;
  32.                         r.notify();
  33.                 }
  34.                 }
  35.         }
  36. }
  37. class Output implements Runnable{
  38.         private Res r;
  39.         Output(Res r){
  40.                 this.r = r;
  41.         }
  42.         public void run(){
  43.                
  44.                 while(true){
  45.                         synchronized(r){
  46.                         if(!r.flag)
  47.                                 try {
  48.                                         r.wait();
  49.                                 } catch (InterruptedException e) {
  50.                                         // TODO Auto-generated catch block
  51.                                         //e.printStackTrace();
  52.                                 }
  53.                         System.out.println(r.name+"......."+r.sex);
  54.                         r.flag = false;
  55.                         r.notify();
  56.                 }
  57.         }
  58.         
  59.         }
  60. }
  61. public class InputOutputDemo {

  62.         public static void main(String[] args) {
  63.                 // TODO Auto-generated method stub
  64.                 Res r = new Res();
  65.                
  66.                 Input in = new Input(r);
  67.                 Output out = new Output(r);
  68.                
  69.                 Thread t1 = new Thread(in);
  70.                 Thread t2 = new Thread(out);
  71.                
  72.                 t1.start();
  73.                 t2.start();
  74.                
  75.         }

  76. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

1 个回复

倒序浏览
1. wait(); 可以让当前线程处于等待,这时的线程被临时存储到了线程池中 ;
   
  2. notify();唤醒线程池中任意一个等待的线程。

  3.  notifyAll();唤醒线程池中所有的等待线程。

        这些方法在使用时,必须定义在同步中,必须被所属同步的锁调用。

      如果程序运行到wait();就会进入到线程池中,放弃了CUP的执行权,
当运行到notify();时,是从线程池中唤醒任意一个线程。

还有一点要注意,就是要声明异常;

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马