java.lang.Object
g1101_1200.s1115_print_foobar_alternately.FooBar

public class FooBar extends Object
1115 - Print FooBar Alternately.<p>Medium</p> <p>Suppose you are given the following code:</p> <p>class FooBar { public void foo() { for (int i = 0; i < n; i++) { print(&ldquo;foo&rdquo;); } } public void bar() { for (int i = 0; i < n; i++) { print(&ldquo;bar&rdquo;); } } }</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1</p> <p><strong>Output:</strong> &ldquo;foobar&rdquo;</p> <p><strong>Explanation:</strong> There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar(). &ldquo;foobar&rdquo; is being output 1 time.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 2</p> <p><strong>Output:</strong> &ldquo;foobarfoobar&rdquo;</p> <p><strong>Explanation:</strong> &ldquo;foobar&rdquo; is being output 2 times.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 1000</code></li> </ul>