程序是练出来的

专注JAVA,JavaScript,Python,三剑走天涯。 软素质:忠心、追求、进取、态度、方法、恒心、坚持

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

开启两个线程,一个线程打印A~Z,一个线程打印1~52的数据

 

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class D20190803  {

      private  char a='A';
      private Lock lock = new ReentrantLock();
      private Condition c_one=lock.newCondition();
      private Condition c_two=lock.newCondition();
      private static boolean flag=true;
    
      Thread letter=new Thread(new Runnable() {
          
           public void run() {
             
               for (int i=0;i<26;i++) {
                  
                 lock.lock();  
             
                   try {
                      
                    if(flag) {
                        
                        c_two.await();
                        
                    }
                    
                     System.out.print("【"+a+"】");
                       
                       a++;
                    
                    Thread.sleep(100);
                    flag=true;
                    c_one.signal();
                    
                    
                } catch (InterruptedException e) {
                    
                    e.printStackTrace();
                }finally {
                    
                    lock.unlock();
                }
                     
               }
               
           }
      });
      
      
      Thread number=new Thread(new Runnable() {
          
           public void run() {
               
               for (int j=1;j<53;j++) {
                   
                   lock.lock();
                   
                   System.out.print(j+",");
                   
                   if(j%2==0 ) {
                      
                       try {
                           

                            Thread.sleep(100);
                            c_two.signal();
                            flag=false;
                              
                       
                        if(!flag) {
                            
                            c_one.await();
                        }
                        
                        
            
                        
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }finally {
                        
                        lock.unlock();
                    }
                           
                    } 
                             
                   }
                   
                  
               }
               
      });
     
    public static void main(String[] args) throws InterruptedException {
        
        D20190803 d=new D20190803();
        d.letter.start();
        Thread.sleep(1000);
        d.number.start();
        
        
    }
    
}

 

 

 

posted on 2019-08-04 18:44  爱学习的小象  阅读(642)  评论(0编辑  收藏  举报