Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
tron-explore
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
张建华@五瓣科技
tron-explore
Commits
ad2abe74
Commit
ad2abe74
authored
Dec 04, 2020
by
jianhua.zhang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
线程池调整:调整为单例模式
parent
346fa147
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
0 deletions
+85
-0
PersistThreadPool.java
.../java/com/wuban/tron/explore/fetch/PersistThreadPool.java
+1
-0
PersistThreadPoolV2.java
...ava/com/wuban/tron/explore/fetch/PersistThreadPoolV2.java
+84
-0
No files found.
src/main/java/com/wuban/tron/explore/fetch/PersistThreadPool.java
View file @
ad2abe74
...
@@ -17,6 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger;
...
@@ -17,6 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger;
*
*
*/
*/
@Slf4j
@Slf4j
@Deprecated
public
class
PersistThreadPool
{
public
class
PersistThreadPool
{
private
static
final
int
POLL_SIZE
=
1
;
private
static
final
int
POLL_SIZE
=
1
;
...
...
src/main/java/com/wuban/tron/explore/fetch/PersistThreadPoolV2.java
0 → 100644
View file @
ad2abe74
package
com
.
wuban
.
tron
.
explore
.
fetch
;
import
com.wuban.tron.explore.util.ThreadPoolUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.ThreadFactory
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
* @author sky
*
*/
@Slf4j
public
class
PersistThreadPoolV2
{
private
static
final
int
POLL_SIZE
=
2
;
private
static
PersistThreadPoolV2
instance
;
public
final
List
<
ThreadPoolExecutor
>
executors
=
new
ArrayList
<>(
POLL_SIZE
);
private
PersistThreadPoolV2
()
{
for
(
int
i
=
0
;
i
<
POLL_SIZE
;
i
++)
{
executors
.
add
(
new
ThreadPoolExecutor
(
POLL_SIZE
,
POLL_SIZE
*
2
,
10L
,
TimeUnit
.
SECONDS
,
new
LinkedBlockingQueue
<>(),
new
PersistThreadFactory
(
i
)));
}
log
.
info
(
"数据持久化-线程池 name:{} coreSize:{} maxSize:{}"
,
"PersistThreadPool"
,
POLL_SIZE
*
POLL_SIZE
,
POLL_SIZE
*
POLL_SIZE
*
2
);
}
public
static
PersistThreadPoolV2
getInstance
()
{
if
(
instance
==
null
)
{
synchronized
(
PersistThreadPoolV2
.
class
)
{
if
(
instance
==
null
)
{
instance
=
new
PersistThreadPoolV2
();
}
}
}
return
instance
;
}
public
void
stop
()
{
for
(
final
ThreadPoolExecutor
executor
:
executors
)
{
ThreadPoolUtil
.
ensureShutdown
(
executor
);
}
}
public
ThreadPoolExecutor
getPool
()
{
Random
random
=
new
Random
();
int
index
=
random
.
nextInt
(
POLL_SIZE
);
return
executors
.
get
(
index
);
}
/**
* The persist thread factory
*/
static
class
PersistThreadFactory
implements
ThreadFactory
{
private
static
final
AtomicInteger
POOL_NUMBER
=
new
AtomicInteger
(
1
);
private
final
ThreadGroup
group
;
private
final
AtomicInteger
threadNumber
=
new
AtomicInteger
(
1
);
private
final
String
namePrefix
;
PersistThreadFactory
(
final
int
index
)
{
final
SecurityManager
s
=
System
.
getSecurityManager
();
this
.
group
=
(
s
!=
null
)
?
s
.
getThreadGroup
()
:
Thread
.
currentThread
().
getThreadGroup
();
this
.
namePrefix
=
"persist-pool"
+
index
+
"-"
+
POOL_NUMBER
.
getAndIncrement
()
+
"-thread-"
;
}
@Override
public
Thread
newThread
(
final
Runnable
r
)
{
final
Thread
t
=
new
Thread
(
this
.
group
,
r
,
this
.
namePrefix
+
this
.
threadNumber
.
getAndIncrement
(),
0
);
if
(
t
.
isDaemon
())
{
t
.
setDaemon
(
false
);
}
if
(
t
.
getPriority
()
!=
Thread
.
NORM_PRIORITY
)
{
t
.
setPriority
(
Thread
.
NORM_PRIORITY
);
}
return
t
;
}
}
}
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