Skip to content

Instantly share code, notes, and snippets.

@mountcedar
Created January 22, 2014 06:36
Show Gist options
  • Save mountcedar/8554348 to your computer and use it in GitHub Desktop.
Save mountcedar/8554348 to your computer and use it in GitHub Desktop.
Guavaライブラリのconcurrentパッケージの使い方 ref: http://qiita.com/mountcedar/items/e20251b811b491666d48
URL[] topSites = null;
try {
topSites = new URL[] {new URL("http://www.google.com")};
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
logger.debug("in main");
ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
for (final URL siteUrl : topSites) {
final ListenableFuture<String> future = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
logger.debug("in call()");
Thread.sleep(100);
return siteUrl.toString();
}
});
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[1]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[2]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
pool.shutdownNow();
protected static Logger logger = LoggerFactory.getLogger(App.class);
public static void main (String[] args) {
URL[] topSites = null;
try {
topSites = new URL[] {new URL("http://www.google.com")};
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
logger.debug("in main");
ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
for (final URL siteUrl : topSites) {
final ListenableFuture<String> future = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
logger.debug("in call()");
Thread.sleep(100);
return siteUrl.toString();
}
});
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[1]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[2]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
}
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
pool.shutdownNow();
14:55:58.286 [main] DEBUG concurrent.App - in main
14:55:58.300 [pool-1-thread-1] DEBUG concurrent.App - in call()
14:55:58.401 [pool-1-thread-2] DEBUG concurrent.App - listener[1]: http://www.google.com
14:55:58.401 [pool-1-thread-3] DEBUG concurrent.App - listener[2]: http://www.google.com
14:55:58.502 [pool-1-thread-3] DEBUG concurrent.App - job completed.
14:55:58.502 [pool-1-thread-2] DEBUG concurrent.App - job completed.
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[2]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, MoreExecutors.sameThreadExecutor());
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[2]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, MoreExecutors.sameThreadExecutor());
15:14:42.126 [main] DEBUG concurrent.App - in main
15:14:42.141 [pool-1-thread-1] DEBUG concurrent.App - in call()
15:14:42.242 [pool-1-thread-1] DEBUG concurrent.App - listener[1]: http://www.google.com
15:14:42.343 [pool-1-thread-1] DEBUG concurrent.App - job completed.
15:14:42.343 [pool-1-thread-1] DEBUG concurrent.App - listener[2]: http://www.google.com
15:14:42.444 [pool-1-thread-1] DEBUG concurrent.App - job completed.
ListeningExecutorService pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
final ListenableFuture<String> future = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
logger.debug("in call()");
Thread.sleep(100);
return siteUrl.toString();
}
});
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[1]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
future.addListener(new Runnable() {
@Override
public void run() {
try {
final String contents = future.get();
logger.debug("listener[1]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}, pool);
public class DataClowler implements Callable<String> {
protected static Logger logger = LoggerFactory.getLogger(DataClowler.class);
protected ListenableScheduledFuture future = null;
protected ListeningScheduledExecutorService scheduler = null;
protected ListeningExecutorService pool = null;
protected static final int threadPoolNum = 10;
protected long delay = 0;
protected String uri = null;
public DataClowler(String url) {
this.scheduler = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(threadPoolNum));
this.pool = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(threadPoolNum));
this.uri = url;
this.delay = 2000; // msec
}
public String call () {
try {
String resource = Resources.toString(new URL(this.uri), Charsets.UTF_8);
future = scheduler.schedule(this, this.delay, TimeUnit.MILLISECONDS);
future.addListener(new Echo(future), this.pool);
return resource;
} catch (Exception e) {
logger.error("Error: {}", e);
return null;
}
}
/**
* タスクを開始(再開)する。
*/
public void start() {
future = scheduler.schedule(this, this.delay, TimeUnit.MILLISECONDS);
future.addListener(new DataClowler(core, future), this.pool);
}
/**
* schedulerに登録したタスクを停止する。
*/
public void stop() {
if (future != null) future.cancel(true);
}
/**
* schedulerをシャットダウンする
*/
public void shutdown() {
scheduler.shutdownNow();
}
}
public class Echo implements Runnable {
protected ListenableScheduledFuture future = null;
public Echo(ListenableScheduledFuture future) {
this.future = future;
}
@Override
public void run() {
try {
final String contents = (String)future.get();
logger.debug("listener[1]: {}", contents);
Thread.sleep(100);
} catch (InterruptedException e) {
} catch (ExecutionException e) {
} finally {
logger.debug("job completed.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment