Java并发 专题
您的位置:java > Java并发专题 > Java并发Condition接口
Java并发Condition接口
作者:--    发布时间:2019-11-20

java.util.concurrent.locks.condition接口提供一个线程挂起执行的能力,直到给定的条件为真。 condition对象必须绑定到lock,并使用newcondition()方法获取对象。

condition类的方法

以下是condition类中可用的重要方法的列表。

序号 方法名称 描述
1 public void await() 使当前线程等待,直到发出信号或中断信号。
2 public boolean await(long time, timeunit unit) 使当前线程等待直到发出信号或中断,或指定的等待时间过去。
3 public long awaitnanos(long nanostimeout) 使当前线程等待直到发出信号或中断,或指定的等待时间过去。
4 public long awaituninterruptibly() 使当前线程等待直到发出信号。
5 public long awaituntil() 使当前线程等待直到发出信号或中断,或者指定的最后期限过去。
6 public void signal() 唤醒一个等待线程。
7 public void signalall() 唤醒所有等待线程。

实例

以下testthread程序演示了condition接口的这些方法。这里我们使用signal()通知和await()挂起线程。

import java.util.concurrent.locks.condition;
import java.util.concurrent.locks.lock;
import java.util.concurrent.locks.reentrantlock;

public class testthread {

   public static void main(string[] args) throws interruptedexception{
      itemqueue itemqueue = new itemqueue(10);

      //create a producer and a consumer.
      thread producer = new producer(itemqueue);
      thread consumer = new consumer(itemqueue);

      //start both threads.
      producer.start();
      consumer.start();

      //wait for both threads to terminate.
      producer.join();
      consumer.join();
   }

   static class itemqueue {

      private object[] items = null;
      private int current = 0;
      private int placeindex = 0;
      private int removeindex = 0;

      private final lock lock;
      private final condition isempty;
      private final condition isfull;

      public itemqueue(int capacity) {
         this.items = new object[capacity];
         lock = new reentrantlock();
         isempty = lock.newcondition();
         isfull = lock.newcondition();
      }

      public void add(object item) throws interruptedexception {
         lock.lock();
         while(current >= items.length)
            isfull.await();

         items[placeindex] = item;

         placeindex = (placeindex + 1) % items.length;

         ++current;

         //notify the consumer that there is data available.
         isempty.signal();

         lock.unlock();
      }

      public object remove() throws interruptedexception {
         object item = null;

         lock.lock();
         while(current <= 0){
            isempty.await();
         }
         item = items[removeindex];

         removeindex = (removeindex + 1) % items.length;

         --current;

         //notify the producer that there is space available.
         isfull.signal();
         lock.unlock();

         return item;
      }

      public boolean isempty(){
         return (items.length == 0);
      }
   }

   static class producer extends thread {

      private final itemqueue queue;
      public producer(itemqueue queue) {
         this.queue = queue;
      }

      @override
      public void run() {
         string[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};

         try {
            for(string number: numbers){
               queue.add(number);
               system.out.println("[producer]: " + number);
            }
            queue.add(null);
         }
         catch (interruptedexception ex) {
            ex.printstacktrace();
         } 
      }
   }

   static class consumer extends thread {

      private final itemqueue queue;
      public consumer(itemqueue queue) {
         this.queue = queue;
      }

      @override
      public void run() {
         try {
            do {
               object number = queue.remove();
               system.out.println("[consumer]: " + number);
               if(number == null){
                  return;
               }
            } while(!queue.isempty());
         }
         catch (interruptedexception ex) {
            ex.printstacktrace();
         }
      }
   }
}

这将产生以下结果。

[producer]: 1
[consumer]: 1
[producer]: 2
[consumer]: 2
[producer]: 3
[consumer]: 3
[producer]: 4
[consumer]: 4
[producer]: 5
[producer]: 6
[consumer]: 5
[producer]: 7
[consumer]: 6
[consumer]: 7
[producer]: 8
[consumer]: 8
[producer]: 9
[consumer]: 9
[producer]: 10
[consumer]: 10
[producer]: 11
[consumer]: 11
[producer]: 12
[consumer]: 12
[consumer]: null

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册