多线程顺序打印ABC

Lock+CAS

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用Lock+CAS实现
 */
public class Main {
    public static void main(String[] args) {
        run();
    }

    private static AtomicInteger integer = new AtomicInteger(1);
    private static int countA = 0;
    private static int countB = 0;
    private static int countC = 0;

    /**
     * ABC每个打印机
     */
    private static int perCount=100;
    private static CountDownLatch countDownLatch=new CountDownLatch(perCount*3);
    private static Lock lock=new ReentrantLock();

    private static void run() {

        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 10, 0L, TimeUnit.MICROSECONDS, new LinkedBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy());


        for (int i = 0; i < perCount*3; i++) {
            executor.execute(new WirteTask());
        }
        try {
            countDownLatch.await();
            System.out.println("打印结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("A->"+countA);
        System.out.println("B->"+countB);
        System.out.println("C->"+countC);
    }

   static class WirteTask implements Runnable {

        public void run() {
            lock.lock();
            try {
                int old = integer.getAndIncrement();
                if (old == 1) {
                    countA++;
                    System.out.println("A->"+countA);
                } else if (old == 2) {
                    countB++;
                    System.out.println("B->"+countB);
                } else if (old == 3) {
                    countC++;
                    System.out.println("C->"+countC);
                    integer.set(1);
                }
                countDownLatch.countDown();
            }finally {
                lock.unlock();
            }


        }
    }
}

Lock+Condition

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main2   {
   private static Lock lock=new ReentrantLock();
   private static Condition conditionA=lock.newCondition();
   private static Condition conditionB=lock.newCondition();
   private static Condition conditionC=lock.newCondition();

   private static int currentNum=1;
    public static void main(String[] args) {
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                printA();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                printB();
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                printC();
            }
        }).start();
    }

    private static void printA() {
        lock.lock();
        System.out.println("A获取到锁");
        try {
            if ((currentNum%3)!=1){
                try {
                    conditionA.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            currentNum++;
            System.out.println("A");
            conditionB.signal();
        }finally {
            lock.unlock();
        }


    }
    private static void printB() {
        lock.lock();
        try {
            System.out.println("B获取到锁");

            if ((currentNum%3)!=2){
                try {
                    conditionB.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            currentNum++;
            System.out.println("B");
            conditionC.signal();
        }finally {

            lock.unlock();
        }

    }
    private static void printC() {
       lock.lock();
       try {
           System.out.println("C获取到锁");

           if ((currentNum%3)!=0){
               try {
                   conditionC.await();
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           currentNum++;
           System.out.println("C");
           conditionA.signal();
       }finally {
           lock.unlock();
       }

    }
}

你可能感兴趣的:(并发编程)