zoukankan      html  css  js  c++  java
  • 多线程通讯之共享内存二(可重用锁)

    1.三个线程交替各打印15次,如此循环10次。

    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    
    public class ReentrantLockAndThreadTask {
        //三个任务依次执行15次,共执行10轮
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            final Transaction tran = new Transaction();
            
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    for (int i = 0 ; i < 15; i ++){
                        tran.sub2(i);
                    }
                }
            }).start();
            new Thread(new Runnable() {
                        
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    for (int i = 0 ; i < 15; i ++){
                        tran.sub3(i);
                    }
                }
            }).start();
            for (int i = 0 ; i < 15; i ++){
                tran.sub1(i);
            }
        }
    
    }
    class Transaction{
        private int turn = 1;
        ReentrantLock tranLock = new ReentrantLock(true);
        Condition condition1 = tranLock.newCondition();
        Condition condition2 = tranLock.newCondition();
        Condition condition3 = tranLock.newCondition();
        public void sub1(int j){
            tranLock.lock();
            try {
                while (turn != 1){
                    //如果不是第一个线程的轮次就等待
                    condition1.await();
                    System.out.println("111");
                }
                for (int i = 0 ; i < 15; i ++){
                    System.out.println(Thread.currentThread().getName()+"正在执行第:"+j+"次序列号是:"+i);;
                }
                turn = 2;
                condition2.signal();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                tranLock.unlock();
            }
        }
        public void sub2(int j){
            boolean ishold = tranLock.isHeldByCurrentThread();
            int num = tranLock.getHoldCount();
            tranLock.lock();
            int num1 = tranLock.getHoldCount();
            boolean ishold1 = tranLock.isHeldByCurrentThread();
            try {
                while (turn != 2){
                    //如果不是第一个线程的轮次就等待
                    condition2.await();
                    System.out.println("222");
                }
                for (int i = 0 ; i < 15; i ++){
                    System.out.println(Thread.currentThread().getName()+"正在执行第:"+j+"次序列号是:"+i);;
                }
                turn = 3;
                condition3.signal();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                tranLock.unlock();
            }
        }
        public void sub3(int j){
            tranLock.lock();
            try {
                while (turn != 3){
                    //如果不是第一个线程的轮次就等待
                    condition3.await();
                    System.out.println("333");
                }
                for (int i = 0 ; i < 15; i ++){
                    System.out.println(Thread.currentThread().getName()+"正在执行第:"+j+"次序列号是:"+i);;
                }
                turn = 1;
                condition1.signal();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                tranLock.unlock();
            }
        }
    }
  • 相关阅读:
    SD卡测试
    测试人员可能会遇到的问题
    HDU 1024 Max Sum Plus Plus
    HDU 1176 免费馅饼
    HDU 1257 最少拦截系统
    HDU 1087 Super Jumping! Jumping! Jumping!
    poj 1328 Radar Installation
    poj 1753 Flip Game
    HDU 1003 Max Sum
    HDU 5592 ZYB's Premutation(BestCoder Round #65 C)
  • 原文地址:https://www.cnblogs.com/fengyan20150508/p/7840372.html
Copyright © 2011-2022 走看看