zoukankan      html  css  js  c++  java
  • 一个线程间的通讯小程序__(Java_Thread_Inout.Output)

      1 //多线程通讯
      2 //多个线程处理同一资源,但是任务不同
      3 //等待唤醒方法:
      4 //wait():将线程变成为冻结状态,线程会被存储在线程池中;
      5 //notify():唤醒线程中的一个线程(任意的)
      6 //notifyAll():唤醒所有线程;
      7 /**************************************************************/
      8 //建立资源类
      9 class Resource
     10 {
     11     private boolean flag = false;
     12     private String name;
     13     private String sex;
     14     public synchronized void set(String name,String sex)
     15     {
     16         if(flag)
     17             try
     18             {
     19                 this.wait();
     20             }
     21             catch (InterruptedException e)
     22             {
     23             }
     24         this.name = name;
     25         this.sex = sex;
     26         this.flag=true;
     27         this.notify();    
     28     }
     29     public synchronized void get()
     30     {
     31         if(!this.flag)
     32             try
     33             {
     34                 this.wait();
     35             }
     36             catch (InterruptedException e)
     37             {
     38             }
     39         System.out.println(name+"--"+sex); 
     40         this.flag=false;
     41         this.notify();
     42     }
     43 }
     44 //建立输入任务类
     45 class Input implements Runnable
     46 {
     47     Resource r;
     48     Input(Resource r)
     49     {
     50         this.r = r;
     51     }
     52     public void run()
     53     {
     54         int x = 0;
     55         while(true)
     56         {
     57                     if (x==0)
     58                     {
     59                         r.set("野兽","男  ");
     60                     }
     61                     else
     62                     {
     63                         r.set("meinv","nv  ");
     64                     }
     65                     x=(x+1)%2;
     66         }
     67     }
     68 }
     69 //建立输出任务类
     70 class Output implements Runnable
     71 {
     72     Resource r;
     73     Output(Resource r)
     74     {
     75         this.r = r;
     76     }
     77             public void run()
     78             {
     79                 while(true)
     80                 {
     81                     synchronized(r)
     82                     {    
     83                             r.get();
     84                     }
     85                 }
     86     
     87              }
     88 }
     89 
     90 class  IoDemo1
     91 {
     92     public static void main(String[] args) 
     93     {
     94         //建立资源对象
     95         Resource r = new Resource();
     96           //建立输入任务对象
     97         Input in = new Input(r);
     98         //建立输出任务对象
     99         Output out = new Output(r);
    100         //建立输入任务的进程
    101         Thread t1 = new Thread(in);
    102         //建立输出任务的进程
    103         Thread t2 = new Thread(out);
    104         //开启线程
    105         t1.start();
    106         t2.start();
    107     }
    108 }
  • 相关阅读:
    高斯消元(学习笔记)
    离散化(学习笔记)
    并查集(学习笔记)
    模板---负环(学习笔记)
    差分数组 前缀和(学习笔记)
    manacher马拉车算法(学习笔记)
    Java 常用对象-Object类
    Java 值传递和引用传递
    Java 集合-Set接口和三个子类实现
    Java 集合-Arrays工具类的介绍
  • 原文地址:https://www.cnblogs.com/tabchanj/p/5389750.html
Copyright © 2011-2022 走看看