Commit 3c79d031 authored by yihua.huang's avatar yihua.huang

fix thread pool

parent 81f75347
...@@ -234,12 +234,15 @@ public class Spider implements Runnable, Task { ...@@ -234,12 +234,15 @@ public class Spider implements Runnable, Task {
} }
Request request = scheduler.poll(this); Request request = scheduler.poll(this);
//single thread //single thread
if (executorService == null) { if (threadNum <= 1) {
while (request != null && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) { while (request != null && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) {
processRequest(request); processRequest(request);
request = scheduler.poll(this); request = scheduler.poll(this);
} }
} else { } else {
synchronized (this) {
this.executorService = ThreadUtils.newFixedThreadPool(threadNum);
}
//multi thread //multi thread
final AtomicInteger threadAlive = new AtomicInteger(0); final AtomicInteger threadAlive = new AtomicInteger(0);
while (true && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) { while (true && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) {
...@@ -363,10 +366,11 @@ public class Spider implements Runnable, Task { ...@@ -363,10 +366,11 @@ public class Spider implements Runnable, Task {
public void stop() { public void stop() {
stat.compareAndSet(STAT_RUNNING, STAT_STOPPED); stat.compareAndSet(STAT_RUNNING, STAT_STOPPED);
executorService.shutdown();
} }
public void stopAndDestroy() { public void stopAndDestroy() {
stat.compareAndSet(STAT_RUNNING, STAT_STOPPED); stop();
destroy(); destroy();
} }
...@@ -385,9 +389,6 @@ public class Spider implements Runnable, Task { ...@@ -385,9 +389,6 @@ public class Spider implements Runnable, Task {
if (threadNum == 1) { if (threadNum == 1) {
return this; return this;
} }
synchronized (this) {
this.executorService = ThreadUtils.newFixedThreadPool(threadNum);
}
return this; return this;
} }
......
...@@ -44,9 +44,4 @@ public abstract class Selectors { ...@@ -44,9 +44,4 @@ public abstract class Selectors {
return new OrSelector(selectors); return new OrSelector(selectors);
} }
public static void main(String[] args) {
String s = "a";
or(regex("<title>(.*)</title>"), xpath("//title"), $("title")).select(s);
}
} }
\ No newline at end of file
...@@ -16,8 +16,6 @@ import java.util.regex.Pattern; ...@@ -16,8 +16,6 @@ import java.util.regex.Pattern;
*/ */
public class UrlUtils { public class UrlUtils {
private static Pattern relativePathPattern = Pattern.compile("^([\\.]+)/");
/** /**
* canonicalizeUrl * canonicalizeUrl
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment