本文主要是介绍多线程按序输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
我们提供了一个类: public class Foo { public void first() { print(“first”); }
public void second() { print(“second”); } public void third() {
print(“third”); } } 三个不同的线程 A、B、C 将会共用一个 Foo 实例。一个将会调用 first() 方法 一个将会调用 second() 方法 还有一个将会调用 third() 方法 请设计修改程序,以确保
second() 方法在 first() 方法之后被执行,third() 方法在 second() 方法之后被执行。
力扣(LeetCode)
使用原子类实现
class Foo {private AtomicInteger atomicInteger = new AtomicInteger(0);public Foo() { }public void first(Runnable printFirst) throws InterruptedException {// printFirst.run() outputs "first". Do not change or remove this line.printFirst.run();ato
这篇关于多线程按序输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!