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