1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (English)/交替打印 FooBar(English) [print-foobar-alternately].html
2022-03-29 12:43:11 +08:00

52 lines
1.2 KiB
HTML

<p>Suppose you are given the following code:</p>
<pre>
class FooBar {
public void foo() {
for (int i = 0; i &lt; n; i++) {
print(&quot;foo&quot;);
}
}
public void bar() {
for (int i = 0; i &lt; n; i++) {
print(&quot;bar&quot;);
}
}
}
</pre>
<p>The same instance of <code>FooBar</code> will be passed to two different threads:</p>
<ul>
<li>thread <code>A</code> will call <code>foo()</code>, while</li>
<li>thread <code>B</code> will call <code>bar()</code>.</li>
</ul>
<p>Modify the given program to output <code>&quot;foobar&quot;</code> <code>n</code> times.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> &quot;foobar&quot;
<strong>Explanation:</strong> There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
&quot;foobar&quot; is being output 1 time.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> &quot;foobarfoobar&quot;
<strong>Explanation:</strong> &quot;foobar&quot; is being output 2 times.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 1000</code></li>
</ul>