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 }