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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

[Java] 纯文本查看 复制代码
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionDemo {
        private final Lock lock = new ReentrantLock(); 
        private final Condition isFull = lock.newCondition();
        private final Condition isEmtry = lock.newCondition();
        private String[] data = new String[10];
        private int len, getStr,putStr;
        private StringBuilder buff = new StringBuilder();
        public void putData(){
                lock.lock();
                System.out.println("正在填充数据...");
                try{
                        while(len==data.length){
                                System.out.println(Thread.currentThread().getName()+" 填充完毕...");  // 这里无法打印
                                putStr=buff.length();
                                try{
                                        isFull.await();
                                }catch(InterruptedException e){
                                        System.err.println(e.toString());
                                }
                        }
                        while(putStr<=data.length){
                        if(putStr==data.length){
                                putStr = 0;
                                break;
                        }
                        data[putStr] = String.valueOf(new Random().nextInt(100));
                        System.out.println("data["+putStr+"]="+data[putStr]);
                        ++putStr;
                        ++len;
                        }
                        isEmtry.signal();
                }finally{
                        lock.unlock();
                }
                
        }
        public void getData(){
                lock.lock();
                System.out.println("正在截获信息...");  // 运行到这里,卡住了
                try{
                        while(len==0){                        
                                System.out.println(Thread.currentThread().getName()+"截获数据完毕...");
                                System.out.println(buff.toString());
                                try {
                                        isEmtry.await();
                                } catch (InterruptedException e) {
                                        e.printStackTrace();
                                }
                        }
                 while(getStr<=data.length){
                 if(getStr==data.length){
                         getStr = 0;
                         break;
                 }
                 String info = data[getStr];
                 buff.append(info+",");
             ++getStr;
                 --len;
                 }
                 isFull.signal();
                }finally{
                        lock.unlock();
                }
        }
        public static void main(String[] args) {
           final ConditionDemo condition = new ConditionDemo();
           final CountDownLatch latch = new CountDownLatch(1);
         
           new Thread(new Runnable(){
                   
                  @Override
                  public void run(){
                        condition.putData();
                        latch.countDown();
                  }
           }).start();
           
           try {
                                 latch.await();
                          } catch (InterruptedException e) {                
                                        e.printStackTrace();
                        }
           
              condition.getData();
        }

}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马