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 alternating printing FooBar with leetcode multithreading

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

Share

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

Editor to share with you how to solve the problem of alternating printing FooBar with leetcode multithreading, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's go to know it together.

We provide a class for the title: class FooBar {public void foo () {for (int I = 0; I < n; iTunes +) {print ("foo");}} public void bar () {for (int I = 0; I < n; iTunes +) {print ("bar");} two different threads will share a FooBar instance. One thread will call the foo () method, and the other thread will call the bar () method. Please design a modification program to ensure that "foobar" is output n times. Source: LeetCode link: https://leetcode-cn.com/problems/print-foobar-alternately copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer class FooBar {private int n; ReentrantLock lock = new ReentrantLock (); Condition fooCnd = lock.newCondition (); Condition barCnd = lock.newCondition (); boolean foo = true; public FooBar (int n) {this.n = n;} public void foo (Runnable printFoo) throws InterruptedException {lock.lock (); try {for (int I = 0; I < n) If (! foo) {fooCnd.await ();} foo = false; / / printFoo.run () outputs "foo". Do not change or remove this line. PrintFoo.run (); barCnd.signal ();}} finally {lock.unlock ();}} public void bar (Runnable printBar) throws InterruptedException {lock.lock (); try {for (int I = 0; I < n) ) {if (foo) {barCnd.await ();} foo = true; / / printBar.run () outputs "bar". Do not change or remove this line. PrintBar.run (); fooCnd.signal ();}} finally {lock.unlock ();}

Here, ReentrantLock's condition is used for conditional control.

These are all the contents of this article entitled "how to solve the problem of alternating printing FooBar with 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