Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
5ecd909e
Commit
5ecd909e
authored
Apr 25, 2014
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add timeout for wait/notify #111
parent
964e6372
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
21 deletions
+32
-21
Spider.java
...agic-core/src/main/java/us/codecraft/webmagic/Spider.java
+13
-1
CountableThreadPool.java
...decraft/webmagic/selector/thread/CountableThreadPool.java
+18
-19
SpiderTest.java
...-core/src/test/java/us/codecraft/webmagic/SpiderTest.java
+1
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
View file @
5ecd909e
...
@@ -20,6 +20,7 @@ import java.io.Closeable;
...
@@ -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 {
...
@@ -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 {
...
@@ -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 {
...
@@ -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
;
}
}
}
webmagic-core/src/main/java/us/codecraft/webmagic/selector/thread/CountableThreadPool.java
View file @
5ecd909e
...
@@ -51,9 +51,10 @@ public class CountableThreadPool {
...
@@ -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,6 +62,9 @@ public class CountableThreadPool {
...
@@ -61,6 +62,9 @@ public class CountableThreadPool {
}
catch
(
InterruptedException
e
)
{
}
catch
(
InterruptedException
e
)
{
}
}
}
}
}
finally
{
reentrantLock
.
unlock
();
}
}
}
threadAlive
.
incrementAndGet
();
threadAlive
.
incrementAndGet
();
executorService
.
execute
(
new
Runnable
()
{
executorService
.
execute
(
new
Runnable
()
{
...
@@ -79,11 +83,6 @@ public class CountableThreadPool {
...
@@ -79,11 +83,6 @@ public class CountableThreadPool {
}
}
}
}
});
});
}
finally
{
if
(
reentrantLock
.
isLocked
())
{
reentrantLock
.
unlock
();
}
}
}
}
public
boolean
isShutdown
()
{
public
boolean
isShutdown
()
{
...
...
webmagic-core/src/test/java/us/codecraft/webmagic/SpiderTest.java
View file @
5ecd909e
...
@@ -37,7 +37,7 @@ public class SpiderTest {
...
@@ -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
();
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment