线程通信的两种方法

不同线程间进行通信通常有两种简单方法:
方法一 通过访问共享变量的方式(注:需要处理同步问题)
方法二 通过管道流

其中方法一有两种实现方法,即
方法一a)通过内部类实现线程的共享变量
代码如下:

  1. /**  
  2.  * 通过内部类实现线程的共享变量  
  3.  *  
  4.  */  
  5. public class Innersharethread {   
  6.     public static void main(String[] args) {   
  7.         Mythread mythread = new Mythread();   
  8.         mythread.getThread().start();   
  9.         mythread.getThread().start();   
  10.         mythread.getThread().start();   
  11.         mythread.getThread().start();   
  12.     }   
  13. }   
  14. class Mythread {   
  15.     int index = 0;   
  16.   
  17.     private class InnerThread extends Thread {   
  18.         public synchronized void run() {   
  19.             while (true) {   
  20.                 System.out.println(Thread.currentThread().getName()   
  21.                         + "is running and index is " + index++);   
  22.             }   
  23.         }   
  24.     }   
  25.   
  26.     public Thread getThread() {   
  27.         return new InnerThread();   
  28.     }   
  29. }  
  30. 方法二b)通过实现Runnable接口实现线程的共享变量
    代码如下
    1. /**   
    2. * 通过实现Runnable接口实现线程的共享变量   
    3. * @author Administrator   
    4.  
    5. */    
    6. public class Interfacaesharethread {    
    7. public static void main(String[] args) {    
    8. Mythread mythread = new Mythread();    
    9. new Thread(mythread).start();    
    10. new Thread(mythread).start();    
    11. new Thread(mythread).start();    
    12. new Thread(mythread).start();    
    13. }    
    14. }    
    15.   
    16. /* 实现Runnable接口 */    
    17. class Mythread implements Runnable {    
    18. int index = 0;    
    19.   
    20. public synchronized void run() {    
    21. while (true)    
    22. System.out.println(Thread.currentThread().getName()    
    23. "is running and the index is " + index++);    
    24. }    
    25. }   
    26. 方法二:
      代码如下
    27. import java.io.IOException;
      import java.io.PipedInputStream;
      import java.io.PipedOutputStream;
    28. public class CommunicateWhitPiping {
      public static void main(String[] args) {
      /**
      * 创建管道输出流
      */
      PipedOutputStream pos = new PipedOutputStream();
      /**
      * 创建管道输入流
      */
      PipedInputStream pis = new PipedInputStream();
      try {
      /**
      * 将管道输入流与输出流连接
      * 此过程也可通过重载的构造函数来实现
      */
      pos.connect(pis);
      } catch (IOException e) {
      e.printStackTrace();
      }
      /**
      * 创建生产者线程
      */
      Producer p = new Producer(pos);
      /**
      * 创建消费者线程
      */
      Consumer c = new Consumer(pis);
      /**
      * 启动线程
      */
      p.start();
      c.start();
      }
      }
    29. /**
      * 生产者线程(与一个管道输入流相关联)
      *
      */
      class Producer extends Thread {
      private PipedOutputStream pos;
      public Producer(PipedOutputStream pos) {
      this.pos = pos;
      }
      public void run() {
      int i = 8;
      try {
      pos.write(i);
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
    30. /**
      * 消费者线程(与一个管道输入流相关联)
      *
      */
      class Consumer extends Thread {
      private PipedInputStream pis;
      public Consumer(PipedInputStream pis)
      {
      this.pis = pis;
      }
      public void run() {
      try {
      System.out.println(pis.read());
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值