多线程--多个线程循环顺序的多种实现。

问题:3个线程按顺序打印ABC,A线程打印A,B线程打印B,C线程打印C.

实现一:使用synchronized关键字:

package com.Thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by Administrator on 2018/3/25 0025.
 */
public class Print implements Runnable

{
  private static AtomicInteger count = new AtomicInteger(0);

  public static void main ( String args[] ) {
    Print p1 = new Print();
    Print p2 = new Print();
    Print p3 = new Print();
    Thread t1 = new Thread(p1);
    Thread t2 = new Thread(p2);
    Thread t3 = new Thread(p3);
    t1.start();
    t2.start();
    t3.start();


  }

  public void run () {
    while (true) {
      synchronized (count) {
        try {
          if (count.get() % 3 == 0) {
            System.out.print("A");

          } else if (count.get() % 3 == 1) {
            System.out.print("B");
          } else {
            System.out.print("C");

          }
          count.set(count.get() + 1);
          count.notifyAll();
          count.wait();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
  }
} 

实现二:使用重入锁+Condition

package com.Thread;

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

/**
 * Created by Administrator on 2018/3/25 0025.
 */
public class Print1 implements Runnable {
  static Lock lock = new ReentrantLock();
  static Condition c1 = lock.newCondition();
  static Condition c2 = lock.newCondition();
  static Condition c3 = lock.newCondition();
  static Integer aa=1;

  public static void main ( String args[] ) {

    new Thread(new Print1()).start();
    new Thread(new Print1()).start();
    new Thread(new Print1()).start();

  }

  public void run () {
    for (int i = 1; i < 4; i++) {

        if (aa % 3 == 1) {
          try {
        lock.lock();
        System.out.print("A");
        ++aa;
        c2.signal();
        c1.await();


        } catch (InterruptedException e) {
          e.printStackTrace();
        }finally {
          lock.unlock();
        }
      }
      else if (aa % 3 == 2) {
        try {
        lock.lock();
        System.out.print("B");
        ++aa;

          c3.signal();
          c2.await();
        } catch (InterruptedException e) {
          e.printStackTrace();
        }finally {
          lock.unlock();
        }
      }
      else if (aa % 3 == 0) {
        try {
          lock.lock();
          System.out.print("C");
          ++aa;

          c1.signal();
          c3.await();

        } catch (InterruptedException e) {
          e.printStackTrace();
        }finally {
          lock.unlock();
        }
      }
    }
  }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值