Skip to content

Instantly share code, notes, and snippets.

@kenchan
Created August 19, 2008 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenchan/6154 to your computer and use it in GitHub Desktop.
Save kenchan/6154 to your computer and use it in GitHub Desktop.
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Main {
private static LinkedBlockingQueue<LuceneIndex> queue = new LinkedBlockingQueue<LuceneIndex>();
public static void main(String[] args) throws Exception {
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new PrintThread(), 0, 5, TimeUnit.SECONDS);
for (int i = 1; i < 10000; i++) {
Thread.sleep(1000);
if (Math.random() < 0.5) {
queue.put(new LuceneIndex(Method.CREATE, "" + i));
} else {
queue.put(new LuceneIndex(Method.DELETE, "" + i));
}
}
}
private static class PrintThread implements Runnable {
public void run() {
try {
System.out.println("Thread Start");
while (true) {
System.out.println("インデックス開く");
try {
LuceneIndex i = queue.take();
System.out.println("type:" + i.getMethod() + " name:" + i.getName());
if (Math.random() < 0.2) {
throw new IllegalStateException("");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (RuntimeException e) {
System.out.println("logをはく");
}
}
}
private static enum Method {
CREATE, DELETE;
}
private static class LuceneIndex {
private Method method;
private String name;
public LuceneIndex(Method method, String name) {
this.method = method;
this.name = name;
}
public Method getMethod() {
return this.method;
}
public void setMethod(Method method) {
this.method = method;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment