From 5ecd909ef2282705cb8f6fb3c2accea2f283354d Mon Sep 17 00:00:00 2001 From: "yihua.huang" Date: Fri, 25 Apr 2014 19:37:55 +0800 Subject: [PATCH] add timeout for wait/notify #111 --- .../java/us/codecraft/webmagic/Spider.java | 14 +++++- .../selector/thread/CountableThreadPool.java | 43 +++++++++---------- .../us/codecraft/webmagic/SpiderTest.java | 2 +- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java b/webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java index 0c5b147..81cf179 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java @@ -20,6 +20,7 @@ import java.io.Closeable; import java.io.IOException; import java.util.*; import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.locks.Condition; @@ -104,6 +105,8 @@ public class Spider implements Runnable, Task { private Date startTime; + private int emptySleepTime = 30000; + /** * create a spider with pageProcessor. * @@ -524,7 +527,7 @@ public class Spider implements Runnable, Task { if (threadPool.getThreadAlive() == 0 && exitWhenComplete) { return; } - newUrlCondition.await(); + newUrlCondition.await(emptySleepTime, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { logger.warn("waitNewUrl - interrupted, error {}", e); } finally { @@ -716,4 +719,13 @@ public class Spider implements Runnable, Task { public Scheduler getScheduler() { return scheduler; } + + /** + * Set wait time when no url is polled.

+ * + * @param emptySleepTime In MILLISECONDS. + */ + public void setEmptySleepTime(int emptySleepTime) { + this.emptySleepTime = emptySleepTime; + } } diff --git a/webmagic-core/src/main/java/us/codecraft/webmagic/selector/thread/CountableThreadPool.java b/webmagic-core/src/main/java/us/codecraft/webmagic/selector/thread/CountableThreadPool.java index b20ff15..ac41668 100644 --- a/webmagic-core/src/main/java/us/codecraft/webmagic/selector/thread/CountableThreadPool.java +++ b/webmagic-core/src/main/java/us/codecraft/webmagic/selector/thread/CountableThreadPool.java @@ -51,9 +51,10 @@ public class CountableThreadPool { private ExecutorService executorService; public void execute(final Runnable runnable) { - try { - if (threadAlive.get() >= threadNum) { + + if (threadAlive.get() >= threadNum) { + try { reentrantLock.lock(); while (threadAlive.get() >= threadNum) { try { @@ -61,29 +62,27 @@ public class CountableThreadPool { } catch (InterruptedException e) { } } - } - threadAlive.incrementAndGet(); - executorService.execute(new Runnable() { - @Override - public void run() { - try { - runnable.run(); - } finally { - try { - reentrantLock.lock(); - threadAlive.decrementAndGet(); - condition.signal(); - } finally { - reentrantLock.unlock(); - } - } - } - }); - } finally { - if (reentrantLock.isLocked()) { + } finally { reentrantLock.unlock(); } } + threadAlive.incrementAndGet(); + executorService.execute(new Runnable() { + @Override + public void run() { + try { + runnable.run(); + } finally { + try { + reentrantLock.lock(); + threadAlive.decrementAndGet(); + condition.signal(); + } finally { + reentrantLock.unlock(); + } + } + } + }); } public boolean isShutdown() { diff --git a/webmagic-core/src/test/java/us/codecraft/webmagic/SpiderTest.java b/webmagic-core/src/test/java/us/codecraft/webmagic/SpiderTest.java index 9d950ae..ba29387 100644 --- a/webmagic-core/src/test/java/us/codecraft/webmagic/SpiderTest.java +++ b/webmagic-core/src/test/java/us/codecraft/webmagic/SpiderTest.java @@ -37,7 +37,7 @@ public class SpiderTest { @Test public void testWaitAndNotify() throws InterruptedException { for (int i = 0; i < 10000; i++) { - System.out.println("round" + i); + System.out.println("round " + i); testRound(); } }