java并发编程的艺术-(3)

三、线程间通信

1.等待/通知 机制

调用 obj.wait(); 的线程会立刻释放锁,然后进入 wait状态,等待另一个持有 obj 对象锁的线程调用 obj.notify() 来唤醒。

每个锁对象都具有 一个就绪队列,和一个阻塞队列

  1. java 为超类 Object 实现了 wait( ); notify( ); notifyAll( ); 等方法,以便于当 任一对象作为锁对象时,可以使用等待/通知机制;

  2. wait( ); notify( ); notifyAll( ); 等方法,只能在 非static同步方法 或者 sychronized( 非class对象 ){ } 代码块中使用。

  3. 另一个线程使用 notify( )后,唤醒先前同一锁对象下 wait( ) 的随机一个线程,并且不是立刻唤醒,而是 使用notify( ) 线程执行结束后才唤醒。

  4. 一个 wait( ) 线程被唤醒后 执行完毕只会,也并不会自动唤醒另一个线程,需要手动调用 notify();

  5. 要注意,如果 notify() 在 wait() 之前执行, wait() 的线程将不会被唤醒

    public class ThreadA extends Thread {
    private Object obj;

     public ThreadA(Object obj) {
         this.obj = obj;
     }
    
     @Override
     public void run() {
         super.run();
         try {
             synchronized (obj) {
                 if (MyRepository.count != 5) {
                     System.out.println("线程"+Thread.currentThread().getName()+"开始等待");
                     obj.wait();
                 }
                 System.out.println("线程"+Thread.currentThread().getName()+"结束等待");
             }
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
     }
    

    }

    public class ThreadB extends Thread {
    private Object obj;

     public ThreadB(Object obj) {
         this.obj = obj;
     }
    
     @Override
     public void run() {
         super.run();
         synchronized (obj) {
             for (int i = 0; i < 10 ; i++) {
                 System.out.println("线程"                                  +Thread.currentThread().getName()+":count="+MyRepository.count++);
                 if (MyRepository.count == 5) {
               System.out.println("线程"+Thread.currentThread().getName()+"开始唤醒");
                     obj.notify();
                 }
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         }
     }
    

    }

wait( long ); 这个方法是让线程等待一段时间后被自动唤醒。

2. 生产者/消费者 模式实现

  • 一对一 生产消费模型 使用notify() 即可

  • n对多 生产消费模型 需要使用notifyAll() , 因为如果使用 notify() 会导致连续唤醒同类,而使得都进入wait() 导致该部分程序假死。

  • 注意:使用 if ( 条件 ) { wait( ) },在多线程时,可能导致因为多个等待线程同时被唤醒,而不符合预期的情况,可以使用 while ( 条件 ){ wait( ) } 让线程被唤醒后再次进行等待条件判断。
    生产者与消费者 共有一个容器,
    生产者向容器 add 对象同时唤醒消费者,直到容器装满,生产者进入wait();
    消费者向容器 get and remove对象同时唤醒生产者,直到容器空,消费者进入wait();
    public class Producer {
    private String lock;

        public Producer(String lock) {
            this.lock = lock;
        }
        public void setValue(){
            try {
                synchronized (lock){
                    // 代表 这个容器里的值还没有被消费,就先等待对方消费后唤醒
                    if(!MyRepository.value.equals("")){
                        lock.wait();
                    }
                    String value = System.currentTimeMillis() + "_" + System.nanoTime();
                    MyRepository.value = value;
                    System.out.println("生产者生产的值为"+value);
                    Thread.sleep(1000);
                    // 通知消费者消费
                    lock.notify();
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }
      public class Consumer {
    
          private String lock;
    
          public Consumer(String lock) {
              this.lock = lock;
          }
        
          public void getValue(){
              try {
                  synchronized (lock){
                      // 代表 这个容器里还没有值,就先等待对方生产后唤醒
                      if(MyRepository.value.equals("")){
                          lock.wait();
                      }
                      String value = System.currentTimeMillis() + "_" + System.nanoTime();
                      System.out.println(" 消费到的值是"+MyRepository.value);
                      // 清空容器,模拟remove()
                      MyRepository.value = "";
                      Thread.sleep(1000);
                      // 通知生产者生产
                      lock.notify();
                  }
              }catch (InterruptedException e){
                  e.printStackTrace();
              }
          }
    
      }
    
      public class TestPAndC {
    
          public static void main(String[] args) {
              String lock = "abc123";
              final Producer producer = new Producer(lock);
              final Consumer consumer = new Consumer(lock);
              Thread p = new Thread(new Runnable() {
                  @Override
                  public void run() {
                      while (true){
                          producer.setValue();
                      }
                  }
              });
              Thread c = new Thread(new Runnable() {
                  @Override
                  public void run() {
                      while (true){
                          consumer.getValue();
                      }
                  }
              });
              p.start();
              c.start();
          }
    
      }
    

3.线程间 IO 操作

使用 PipedInputStream & PipedOutputStream 和 PiepedWriter $ PipedReader 让线程之间可以进行IO 操作;

使用 pipedReader.connect(pipedWriter); 创建 输出流和输入流的连接,当 其中一个流 closed() 之后,如果还想继续传输数据,需要重新连接,否则会异常。

package com.sixcity.topic3;

import java.io.*;

public class RandWTest {
    public static void writeData(PipedOutputStream out, String data) {
        try {
            System.out.println("写入到管道流,数据:" + data);
            out.write(data.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void readData(PipedInputStream input) {
        try {
            System.out.println("管道流读取");
            byte[] readArr = new byte[1024];
            int len;
            while ((len = input.read(readArr)) != -1) {
                String s = new String(readArr, 0, len);
                System.out.println("读取数据为:" + s);
                System.out.println("len =" +len);
            }
            System.out.println("reader was closed");
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final PipedInputStream pipedReader = new  PipedInputStream();
        final PipedOutputStream pipedWriter = new PipedOutputStream();
        try {
            pipedReader.connect(pipedWriter);
            Thread outT = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int i = 0; i < 10; i++) {
                            writeData(pipedWriter, "数据" + i);
                            Thread.sleep(1000);
                        }
                        System.out.println("writer was closed");
                        pipedWriter.close();
                    } catch (InterruptedException | IOException e) {
                        e.printStackTrace();
                    }
                }
            });

            Thread inputT = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                            readData(pipedReader);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            outT.start();
            inputT.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4. join( ) 等待一个线程执行结束,再执行后面的代码

join() 方法内部使用的是 wait() 机制 ;

调用方法是 线程 A 内部 执行了 线程b.join() ; 那么线程A 后面的代码会等待 线程b 执行结束后再执行。

这个应用通常用来等待 b线程操作的结果。

package com.sixcity.topic3;

public class JoinTest {

    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("执行一个长时间的线程");
                    Thread.sleep(5000);
                    System.out.println("t1线程执行完毕");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        try {
            t1.start();
            t1.join();
            System.out.println("我在t1执行结束之后才执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

5.join( long ) 和sleep ( long )的区别

  • join( long ) 方法的实现是使用 wait( long );
    因此,会释放当前执行join()代码的线程的锁 ,并且如果 此线程 interrupt() 就会立刻抛出 线程中断 异常。
    注意:‘ 此线程’ 指的是 执行 线程B.join()的线程,‘此线程’ 就是实际调用 wait()的线程
    // 注意 使用的锁对象是 上述线程B的实例
    public final synchronized void join(long millis)
    throws InterruptedException {
    long base = System.currentTimeMillis();
    long now = 0;

            if (millis < 0) {
                throw new IllegalArgumentException("timeout value is negative");
            }
    
            if (millis == 0) {
                while (isAlive()) {
                    wait(0);
                }
            } else {
                while (isAlive()) {
                    long delay = millis - now;
                    if (delay <= 0) {
                        break;
                    }
                    wait(delay);
                    now = System.currentTimeMillis() - base;
                }
            }
        }
    
  • sleep( long ) 并不会释放锁。

6. join( long )后的代码提前执行的意外

在线程A 使用 线程B对象作为锁对象时,注意线程B的join(long)方法,会受到线程A中的 synchronized( B ){ } 的执行时间的影响

现象:

线程 B 的run 方法内使用了 锁对象为 线程B 的同步方法/代码块,

线程 A的run方法也 使用了 锁对象为 线程B 的同步方法/代码块 ,

父级线程执行 线程B.join(long) 后,后面的代码 未等到 线程B结束,就执行了。

原因:

join() 方法是同步方法,此时会与 线程B的 synchronized run()和 线程A的 synchronized( b) { } 竞争 b锁;

join ( ) 由于在父级线程执行 b.start()后立刻执行,通常 join( ) 方法最先得到锁,但是它会立刻 wait(long);

此时释放锁,如果锁被线程 A 先得到,线程A执行完毕后,join()与线程 B.synchronized run()再次竞争得到锁,此时发现 wait(long)已经超时,就会立刻唤醒父级线程,并释放锁;

此时父级线程执行后面的代码,并且线程B.synchronized run()得到锁开始执行。

package com.sixcity.topic3;

public class ThreadBB extends Thread {
    @Override
    synchronized public void run() {
        try {
            System.out.println("线程B开始执行");
            Thread.sleep(7000);
            System.out.println("线程B执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Run1 {
    public static void main(String[] args) throws InterruptedException {
        final Thread b = new ThreadBB();
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (b) {
                    try {
                        System.out.println("线程A开始执行");
                        Thread.sleep(5000);
                        System.out.println("线程A执行结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        a.start();
        b.start();
        b.join(2000);
        System.out.println("main执行结束");
    }
}

输出结果为

线程A执行结束
// join 没有按照预期等待 b 结束后 执行,而是‘提前’执行了
main执行结束
线程B开始执行
线程B执行结束

7. ThreadLocal类的使用

变量值的全局共享可以使用 public static 的方式,所有的线程都可以共享 这个全局变量

而 ThreadLocal 提供的是实现每个线程自己的共享变量,是存储每个线程的私有数据

7.1 使用ThreadLocal

线程A 和 线程B 通过ThreadLocal .get() 只能取得自己 set() 的值

import java.util.Date;

public class ThreadTools {
    public static final ThreadLocal t1 = new ThreadLocal();
    public static final ThreadLocal<Date> t2 = new ThreadLocal<Date>();

}

public class ThreadLocalTest {
    public static void main(String[] args) {
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ThreadTools.t1.set("A的值" + i);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("A的值为:" + ThreadTools.t1.get());
                }
                ThreadTools.t1.get();
            }
        });

        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ThreadTools.t1.set("B的值" + i);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("B的值为:" + ThreadTools.t1.get());
                }
                ThreadTools.t1.get();
            }
        });
        a.start();
        b.start();
    }
}

7.2 使用 extends ThreadLocal类的方式,设置默认值

ThreadLocal . get() 如果没有事先设置值,取出来为 null,为了避免这种情况,可以事先给它设置一个默认值;

package com.sixcity.topic3;

import java.util.Date;

public class ThreadLocalExtDate extends ThreadLocal<Date> {
    @Override
    protected Date initialValue() {
        return new Date();
    }
}

7.3 父、子线程共同使用 InheritableThreadLocal类,让子线程 继承父线程的值

  • 子线程中的这个 InheritableThreadLocal 是在 new Thread() 【子线程】时创建的 父线程 InheritableThreadLocal 的副本,其值等于创建时父线程 InheritableThreadLocal 的值,与 子线程start() 时父线程的值无关。
  • 子线程中修改InheritableThreadLocal 只对自己线程内有效,不会影响父线程和其它使用这个InheritableThreadLocal 的线程
public class ThreadTools {

    public static final InheritableThreadLocal<Date> t4 = new InheritableThreadLocal<Date>();
}


public class ThreadLocalTest {
    public static void main(String[] args) {
        // 父线程设置值
        ThreadTools.t4.set(new Date());
        Thread a = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 使用的值是父线程的值
                    System.out.println("A使用继承类的值"+ThreadTools.t4.get().getTime());
                }
            }
        });

        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
          			// 使用的值是父线程的值
                    System.out.println("B使用继承类的值"+ThreadTools.t4.get().getTime());
                }
            }
        });
        a.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        b.start();
    }
}
  • 在使用子线程的 InheritableThreadLocal时,可以默认的修改从父线程继承来的值
    如果 父线程没有调用过 set(),则父子线程使用的是 initialValue;
    如果 父线程调用过 set(),则父线程使用的是parentValue ,子线程使用的是 childValue;
public class InheritThreadLocalExt extends InheritableThreadLocal {
    @Override
    protected Object childValue(Object parentValue) {
        return parentValue + "子线程添加的值";
    }
		
    @Override
    protected Object initialValue() {
        return "我是默认初始值】";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值