Created
March 6, 2013 13:07
-
-
Save tksmaru/5099185 to your computer and use it in GitHub Desktop.
社内勉強会で30分くらいで書いた。いろいろうんこ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.tksmaru.thread; | |
import java.util.concurrent.BlockingQueue; | |
import java.util.concurrent.LinkedBlockingDeque; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class MultiThreadFizzBuzz { | |
private static final int MAX = 100; | |
private static AtomicInteger counter = new AtomicInteger(0); | |
private static BlockingQueue<Integer> table = new LinkedBlockingDeque<Integer>(); | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Thread producer1 = new Thread(new Producer("Cook 1")); | |
Thread producer2 = new Thread(new Producer("Cook 2")); | |
Thread producer3 = new Thread(new Producer("Cook 3")); | |
producer1.start(); | |
producer2.start(); | |
producer3.start(); | |
Thread consumer1 = new Thread(new Consumer("waiter 1")); | |
Thread consumer2 = new Thread(new Consumer("waiter 2")); | |
Thread consumer3 = new Thread(new Consumer("waiter 3")); | |
consumer1.start(); | |
consumer2.start(); | |
consumer3.start(); | |
} | |
public static class Producer implements Runnable { | |
private String name; | |
public Producer(String name) { | |
this.name = name; | |
} | |
@Override | |
public void run() { | |
int count = 0; | |
for (;;) { | |
count = counter.getAndIncrement(); | |
if (count <= MAX) { | |
table.add(count); | |
System.out.println(name + ":" + count); | |
try { | |
// 処理が早すぎるので仕方なく待つ | |
TimeUnit.MILLISECONDS.sleep(100); | |
} catch (InterruptedException ignore) { | |
} | |
} else { | |
break; | |
} | |
} | |
} | |
} | |
public static class Consumer implements Runnable { | |
private String name; | |
private int stopThreshold = 0; | |
public Consumer(String name) { | |
this.name = name; | |
} | |
@Override | |
public void run() { | |
for(;;) { | |
try { | |
// うんこな待ち方 | |
if (table.isEmpty()) { | |
if (stopThreshold == 3) { | |
break; | |
} | |
stopThreshold++; | |
TimeUnit.MILLISECONDS.sleep(300); | |
continue; | |
} else { | |
// reset | |
stopThreshold = 0; | |
} | |
int count = table.take(); | |
doAction(name, count); | |
} catch (InterruptedException ignore) { | |
} | |
} | |
} | |
} | |
private static void doAction(String name, int count) { | |
if (count % 15 == 0) { | |
System.out.println(" " + name + ":" + count + " : FIZZBUZZ"); | |
} else if (count % 5 == 0) { | |
System.out.println(" " + name + ":" + count + " : BUZZ"); | |
} else if (count % 3 == 0) { | |
System.out.println(" " + name + ":" + count + " : FIZZ"); | |
} else { | |
System.out.println(" " + name + ":" + count); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment