1,问题描述
1115. 交替打印 FooBar
难度:中等
给你一个类:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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"); } } }
|
两个不同的线程将会共用一个 FooBar
实例:
- 线程 A 将会调用
foo()
方法,而
- 线程 B 将会调用
bar()
方法
请设计修改程序,以确保 "foobar"
被输出 n
次。
示例 1:
1 2 3
| 输入:n = 1 输出:"foobar" 解释:这里有两个线程被异步启动。其中一个调用 foo() 方法, 另一个调用 bar() 方法,"foobar" 将被输出一次。
|
示例 2:
1 2 3
| 输入:n = 2 输出:"foobarfoobar" 解释:"foobar" 将被输出两次。
|
提示:
2,初步思考
首先声明,我不会。因为日常工作中使用到的地方少,并且也是用的时候临时查找
看了别人的答案才逐渐会的
3,代码处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
| import java.util.Map; import java.util.concurrent.*; import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.ReentrantLock;
class FooBar_CyclicBarrier { private int n; private CyclicBarrier cb = new CyclicBarrier(2); volatile boolean fooExec = true;
public FooBar_CyclicBarrier(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException { for (int i = 0; i < n; i++) { while (!fooExec) { } printFoo.run(); fooExec = false; try { System.out.println("foo-Up"); cb.await(); System.out.println("foo-Down"); } catch (BrokenBarrierException e) { e.printStackTrace(); } } }
public void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; i++) { try { System.out.println("bar-Up"); cb.await(); System.out.println("bar-Dwon"); } catch (BrokenBarrierException e) { e.printStackTrace(); } printBar.run(); fooExec = true; } } }
class FooBar_Semaphore { private int n; private Semaphore fooSemahore = new Semaphore(1); private Semaphore barSemahore = new Semaphore(0);
public FooBar_Semaphore(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) { fooSemahore.acquire(); printFoo.run(); barSemahore.release(); } }
public void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; i++) { barSemahore.acquire(); printBar.run(); fooSemahore.release(); } } }
class FooBar_yield { private int n; volatile boolean fooExec = true;
public FooBar_yield(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException { for (int i = 0; i < n; ) { System.out.println("foo-i:" + i); if (fooExec) { printFoo.run(); fooExec = false; i++; } else { Thread.yield(); } } }
public void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; ) { System.out.println("bar-i:" + i); if (!fooExec) { printBar.run(); fooExec = true; i++; } else { Thread.yield(); } } } }
class FooBar_ReentrantLock { private int n; private ReentrantLock lock = new ReentrantLock(true); volatile boolean fooExec = true;
public FooBar_ReentrantLock(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException { for (int i = 0; i < n; ) { System.out.println("foo-up-i:" + i); lock.lock(); System.out.println("foo-dw-i:" + i);
try { if (fooExec) { printFoo.run(); fooExec = false; i++; } } finally { lock.unlock(); } } }
public void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; ) { System.out.println("bar-up-i:" + i); lock.lock(); System.out.println("bar-dw-i:" + i); try { if (!fooExec) { printBar.run(); fooExec = true; i++; } } finally { lock.unlock(); } } } }
class FooBar_BlockingQueue { private int n; private BlockingQueue<Integer> fooQueue = new LinkedBlockingQueue<Integer>() {{ add(0); }}; private BlockingQueue<Integer> barQueue = new LinkedBlockingQueue<>();
public FooBar_BlockingQueue(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; i++) { fooQueue.take(); printFoo.run(); barQueue.add(0); } }
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; i++) { barQueue.take(); printBar.run(); fooQueue.add(0); } } }
class FooBar_synchronized { private int n; private Object obj = new Object(); private volatile boolean fooExec = true;
public FooBar_synchronized(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException { for (int i = 0; i < n; i++) { synchronized (obj) { if (!fooExec) { obj.wait(); } printFoo.run(); fooExec = false; obj.notifyAll(); } } }
public void bar(Runnable printBar) throws InterruptedException { for (int i = 0; i < n; i++) { synchronized (obj) { if (fooExec) { obj.wait(); } printBar.run(); fooExec = true; obj.notifyAll(); } } } }
class FooBar_LockSupport { private int n; private Map<String, Thread> map = new ConcurrentHashMap<>(); private volatile boolean fooExec = true;
public FooBar_LockSupport(int n) { this.n = n; }
public void foo(Runnable printFoo) throws InterruptedException { map.put("foo", Thread.currentThread()); for (int i = 0; i < n; i++) { while (!fooExec) { LockSupport.park(); } printFoo.run(); fooExec = false; LockSupport.unpark(map.get("bar")); } }
public void bar(Runnable printBar) throws InterruptedException { map.put("bar", Thread.currentThread()); for (int i = 0; i < n; i++) { while (fooExec) { LockSupport.park(); } printBar.run(); fooExec = true; LockSupport.unpark(map.get("foo")); } } }
public class _1115交替打印FooBar { public static void main(String[] args) throws InterruptedException { FooBar_LockSupport fooBar = new FooBar_LockSupport(2); Thread bar = new Thread(() -> { try { fooBar.bar(() -> System.out.println("bar")); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread foo = new Thread(() -> { try { fooBar.foo(() -> System.out.println("foo")); } catch (InterruptedException e) { e.printStackTrace(); } }); foo.start(); bar.start(); } }
|