java多线程之间的顺序问题

java 多线程:

这样写有问题的:这样写可以的:

package com.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author stono
 * @Date 2018/8/28 下午2:52
 */
public class NULL {
    public static void doWait(CountDownLatch countDownLatch, int count) {
        while (true) {
            long nowCount = countDownLatch.getCount();
            if (nowCount != count) {
                try {
                    countDownLatch.await(1L, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                break;
            }
        }
        countDownLatch.countDown();
    }

    public static void main(String[] args) {

        final CountDownLatch countDownLatch = new CountDownLatch(3);
        System.out.println("start");
        new Thread(new Runnable() {
            public void run() {
                //此处需要执行某代码
                doWait(countDownLatch, 2);
                System.out.println("step 2");
            }

        }).start();
        doWait(countDownLatch, 3);
        System.out.println("step 1");
        new Thread(new Runnable() {
            public void run() {
                //此处需要执行某代码
                doWait(countDownLatch, 1);
                System.out.println("step 3");
            }
        }).start();
        doWait(countDownLatch, 0);
        System.out.println("end");
    }
    //请添加部分代码,使得代码的输出顺序固定为start,step1,step2,step3,end

}

 可以多用几个就不用循环了:

package com.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @Author stono
 * @Date 2018/8/28 下午2:52
 */
public class NULL3 {

    public static void main(String[] args) throws InterruptedException {

        final CountDownLatch downLatch1 = new CountDownLatch(1);
        final CountDownLatch downLatch2 = new CountDownLatch(1);
        final CountDownLatch downLatch3 = new CountDownLatch(1);

        System.out.println("start");
        new Thread(new Runnable() {
            public void run() {
                //此处需要执行某代码
                try {
                    downLatch1.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("step 2");
                downLatch2.countDown();
            }

        }).start();
        System.out.println("step 1");
        downLatch1.countDown();
        new Thread(new Runnable() {
            public void run() {
                //此处需要执行某代码
                try {
                    downLatch2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("step 3");
                downLatch3.countDown();
            }
        }).start();
        downLatch3.await();
        System.out.println("end");
    }
    //请添加部分代码,使得代码的输出顺序固定为start,step1,step2,step3,end

}

 

或者:

public static void main(String[] args) {
  System.out.println("start");

  final StringBuffer sb = new StringBuffer();
  new Thread(new Runnable() {
   @Override
   public void run() {
    while (sb.toString().equals("1")) {
     System.out.println("step 2");
     sb.append("2");
    }
   }
  }).start();

  System.out.println("step 1");
  sb.append("1");

  new Thread(new Runnable() {
   @Override
   public void run() {
    while (sb.toString().equals("12")) {
     System.out.println("step 3");
     sb.append("3");
    }
   }
  }).start();


  while (sb.toString().equals("123")) {
   System.out.println("end");
  }
 }

 可以用phaser:

package com.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * @Author stono
 * @Date 2018/8/28 下午2:52
 */
public class NULL2 {

    public static void main(String[] args) {

        final Phaser phaser2= new Phaser(2);
        final Phaser phaser3 = new Phaser(2);
        final Phaser phaser4 = new Phaser(2);

        System.out.println("start");
        new Thread(new Runnable() {

            public void run() {

                //此处需要执行某代码
                phaser2.arriveAndAwaitAdvance();
                System.out.println("step 2");
                phaser3.arriveAndAwaitAdvance();

            }

        }).start();
        System.out.println("step 1");
        phaser2.arriveAndAwaitAdvance();
        new Thread(new Runnable() {

            public void run() {
                //此处需要执行某代码
                phaser3.arriveAndAwaitAdvance();
                System.out.println("step 3");
                phaser4.arriveAndAwaitAdvance();

            }

        }).start();
        phaser4.arriveAndAwaitAdvance();
        System.out.println("end");
    }

    //请添加部分代码,使得代码的输出顺序固定为start,step1,step2,step3,end

}

 

转载于:https://www.cnblogs.com/stono/p/9548519.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值