Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
PubTools-tools
Project
Project
Details
Activity
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
liuxingyu
PubTools-tools
Commits
2f4431be
Commit
2f4431be
authored
Aug 10, 2023
by
lihao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加超时时间
parent
a80d9858
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
11 deletions
+18
-11
TaskExecutor.java
...utor-kit/src/main/java/com/mushiny/task/TaskExecutor.java
+9
-9
TaskPoolFactory.java
...r-kit/src/main/java/com/mushiny/task/TaskPoolFactory.java
+9
-2
No files found.
task-executor-kit/src/main/java/com/mushiny/task/TaskExecutor.java
View file @
2f4431be
...
...
@@ -22,14 +22,14 @@ public class TaskExecutor<T, U, R> {
private
CountDownLatch
countDownLatch
;
public
TaskExecutor
()
{
int
i
=
Runtime
.
getRuntime
().
availableProcessors
();
int
core
=
Math
.
min
(
i
/
2
,
8
);
this
.
pool
=
new
ThreadPoolExecutor
(
core
,
core
+
1
,
0L
,
TimeUnit
.
MILLISECONDS
,
new
LinkedBlockingQueue
<
Runnable
>());
}
private
Long
countDownTimeOut
;
private
TimeUnit
timeUnit
;
public
TaskExecutor
(
ThreadPoolExecutor
pool
)
{
public
TaskExecutor
(
ThreadPoolExecutor
pool
,
Long
countDownTimeOut
,
TimeUnit
timeUnit
)
{
this
.
pool
=
pool
;
this
.
countDownTimeOut
=
countDownTimeOut
;
this
.
timeUnit
=
timeUnit
;
}
...
...
@@ -62,13 +62,13 @@ public class TaskExecutor<T, U, R> {
}
}));
try
{
countDownLatch
.
await
();
countDownLatch
.
await
(
countDownTimeOut
,
timeUnit
);
return
failCount
.
get
()
<=
0
;
}
catch
(
InterruptedException
e
)
{
return
false
;
}
finally
{
this
.
destroy
();
}
return
failCount
.
get
()
<=
0
;
}
/**
...
...
task-executor-kit/src/main/java/com/mushiny/task/TaskPoolFactory.java
View file @
2f4431be
package
com
.
mushiny
.
task
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
/**
* @author lihao
...
...
@@ -11,10 +13,15 @@ public class TaskPoolFactory {
}
public
static
<
T
,
U
,
R
>
TaskExecutor
<
T
,
U
,
R
>
createExecutor
()
{
return
new
TaskExecutor
<>();
return
createExecutor
(
new
ThreadPoolExecutor
(
2
,
3
,
0L
,
TimeUnit
.
MILLISECONDS
,
new
LinkedBlockingQueue
<>()),
15L
,
TimeUnit
.
SECONDS
);
}
public
static
<
T
,
U
,
R
>
TaskExecutor
<
T
,
U
,
R
>
createExecutor
(
ThreadPoolExecutor
pool
)
{
return
new
TaskExecutor
<>(
pool
);
return
createExecutor
(
pool
,
15L
,
TimeUnit
.
SECONDS
);
}
public
static
<
T
,
U
,
R
>
TaskExecutor
<
T
,
U
,
R
>
createExecutor
(
ThreadPoolExecutor
pool
,
Long
timeOut
,
TimeUnit
timeUnit
)
{
return
new
TaskExecutor
<>(
pool
,
timeOut
,
timeUnit
);
}
}
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