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

博客给出了一个FooBar类代码,包含foo和bar方法。同一FooBar实例会被两个不同线程调用,线程A调用foo,线程B调用bar。要求修改程序使输出‘foobar’ n次,并给出了n为1和2时的输出示例。
1273

被折叠的 条评论
为什么被折叠?



