add timeout for wait/notify #111
parent
964e637264
commit
5ecd909ef2
|
@ -20,6 +20,7 @@ import java.io.Closeable;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.concurrent.locks.Condition;
|
import java.util.concurrent.locks.Condition;
|
||||||
|
@ -104,6 +105,8 @@ public class Spider implements Runnable, Task {
|
||||||
|
|
||||||
private Date startTime;
|
private Date startTime;
|
||||||
|
|
||||||
|
private int emptySleepTime = 30000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create a spider with pageProcessor.
|
* create a spider with pageProcessor.
|
||||||
*
|
*
|
||||||
|
@ -524,7 +527,7 @@ public class Spider implements Runnable, Task {
|
||||||
if (threadPool.getThreadAlive() == 0 && exitWhenComplete) {
|
if (threadPool.getThreadAlive() == 0 && exitWhenComplete) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
newUrlCondition.await();
|
newUrlCondition.await(emptySleepTime, TimeUnit.MILLISECONDS);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.warn("waitNewUrl - interrupted, error {}", e);
|
logger.warn("waitNewUrl - interrupted, error {}", e);
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -716,4 +719,13 @@ public class Spider implements Runnable, Task {
|
||||||
public Scheduler getScheduler() {
|
public Scheduler getScheduler() {
|
||||||
return scheduler;
|
return scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set wait time when no url is polled.<br></br>
|
||||||
|
*
|
||||||
|
* @param emptySleepTime In MILLISECONDS.
|
||||||
|
*/
|
||||||
|
public void setEmptySleepTime(int emptySleepTime) {
|
||||||
|
this.emptySleepTime = emptySleepTime;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,9 +51,10 @@ public class CountableThreadPool {
|
||||||
private ExecutorService executorService;
|
private ExecutorService executorService;
|
||||||
|
|
||||||
public void execute(final Runnable runnable) {
|
public void execute(final Runnable runnable) {
|
||||||
try {
|
|
||||||
|
|
||||||
if (threadAlive.get() >= threadNum) {
|
|
||||||
|
if (threadAlive.get() >= threadNum) {
|
||||||
|
try {
|
||||||
reentrantLock.lock();
|
reentrantLock.lock();
|
||||||
while (threadAlive.get() >= threadNum) {
|
while (threadAlive.get() >= threadNum) {
|
||||||
try {
|
try {
|
||||||
|
@ -61,29 +62,27 @@ public class CountableThreadPool {
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} finally {
|
||||||
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()) {
|
|
||||||
reentrantLock.unlock();
|
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() {
|
public boolean isShutdown() {
|
||||||
|
|
|
@ -37,7 +37,7 @@ public class SpiderTest {
|
||||||
@Test
|
@Test
|
||||||
public void testWaitAndNotify() throws InterruptedException {
|
public void testWaitAndNotify() throws InterruptedException {
|
||||||
for (int i = 0; i < 10000; i++) {
|
for (int i = 0; i < 10000; i++) {
|
||||||
System.out.println("round" + i);
|
System.out.println("round " + i);
|
||||||
testRound();
|
testRound();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue