Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Sequential Printing with leetcode Multithreading

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "leetcode multithreading how to solve the order printing problem", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "leetcode multithreading how to solve the order printing problem" this article.

We provide a class for the title: public class Foo {public void first () {print ("first");} public void second () {print ("second");} public void third () {print ("third");}} three different threads will share a Foo instance. Thread A will call the first () method Thread B will call the second () method Thread C will call the third () method please design the modification program to ensure that the second () method is executed after the first () method and the third () method is executed after the second () method. Source: LeetCode link: https://leetcode-cn.com/problems/print-in-order copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer to the question

CountDownLatch using the juc package

Class Foo {CountDownLatch second = new CountDownLatch (1); CountDownLatch third = new CountDownLatch (1); public Foo () {} public void first (Runnable printFirst) throws InterruptedException {printFirst.run (); second.countDown ();} public void second (Runnable printSecond) throws InterruptedException {second.await (); printSecond.run (); third.countDown () } public void third (Runnable printThird) throws InterruptedException {third.await (); printThird.run ();}} these are all the contents of the article "how to solve the problem of sequential printing in leetcode multithreading". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report