Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
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
YZG
yzg-util
Commits
8d3d3c20
Commit
8d3d3c20
authored
Sep 20, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
升级新版本
parent
b7799941
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
35 deletions
+34
-35
ThreadHelper.java
...c/main/java/com/yanzuoguang/util/thread/ThreadHelper.java
+27
-35
TestTimeout.java
yzg-util-base/src/test/java/helper/TestTimeout.java
+7
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/thread/ThreadHelper.java
View file @
8d3d3c20
...
@@ -4,8 +4,10 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
...
@@ -4,8 +4,10 @@ import com.yanzuoguang.util.exception.ExceptionHelper;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Predicate
;
import
java.util.function.Predicate
;
/**
/**
...
@@ -21,10 +23,12 @@ public class ThreadHelper {
...
@@ -21,10 +23,12 @@ public class ThreadHelper {
private
static
RunPlan
interval
;
private
static
RunPlan
interval
;
private
static
final
long
SECOND_UNIT
=
1000
;
private
static
final
long
SECOND_UNIT
=
1000
;
private
static
final
long
NEXT_SECOND
=
5
;
private
static
final
long
NEXT_SECOND
=
5
;
private
static
final
int
THREAD_SIZE
=
1000
;
/**
/**
* 线程对象
* 线程对象
*/
*/
private
static
final
ExecutorService
executeService
=
Executors
.
newCachedThreadPool
();
private
static
final
ExecutorService
executeService
=
new
ThreadPoolExecutor
(
1
,
THREAD_SIZE
,
1000
,
TimeUnit
.
MILLISECONDS
,
new
ArrayBlockingQueue
<>(
THREAD_SIZE
));
/**
/**
* 初始化线程对象,需要调用该函数之后才能使用
* 初始化线程对象,需要调用该函数之后才能使用
...
@@ -98,10 +102,7 @@ public class ThreadHelper {
...
@@ -98,10 +102,7 @@ public class ThreadHelper {
* 监控线程的方法,防止线程执行死机
* 监控线程的方法,防止线程执行死机
*/
*/
private
static
void
startMonitor
()
{
private
static
void
startMonitor
()
{
runThread
(
new
Runnable
()
{
runThread
(()
->
{
@Override
public
void
run
()
{
do
{
do
{
if
(
threadIsRun
&&
((
System
.
currentTimeMillis
()
-
threadDate
.
getTime
())
/
SECOND_UNIT
)
>
NEXT_SECOND
)
{
if
(
threadIsRun
&&
((
System
.
currentTimeMillis
()
-
threadDate
.
getTime
())
/
SECOND_UNIT
)
>
NEXT_SECOND
)
{
try
{
try
{
...
@@ -116,7 +117,6 @@ public class ThreadHelper {
...
@@ -116,7 +117,6 @@ public class ThreadHelper {
}
}
sleep
(
1000
*
60
);
sleep
(
1000
*
60
);
}
while
(
true
);
}
while
(
true
);
}
});
});
}
}
...
@@ -210,12 +210,7 @@ public class ThreadHelper {
...
@@ -210,12 +210,7 @@ public class ThreadHelper {
* @param run
* @param run
*/
*/
public
static
void
runThread
(
final
Runnable
run
)
{
public
static
void
runThread
(
final
Runnable
run
)
{
executeService
.
execute
(
new
Runnable
()
{
executeService
.
execute
(()
->
executeCatch
(
run
));
@Override
public
void
run
()
{
executeCatch
(
run
);
}
});
}
}
/**
/**
...
@@ -224,9 +219,7 @@ public class ThreadHelper {
...
@@ -224,9 +219,7 @@ public class ThreadHelper {
* @param run
* @param run
*/
*/
public
static
void
runThread
(
final
RunInterval
run
)
{
public
static
void
runThread
(
final
RunInterval
run
)
{
runThread
(
new
Runnable
()
{
runThread
(()
->
{
@Override
public
void
run
()
{
while
(!
run
.
isBreakFlag
())
{
while
(!
run
.
isBreakFlag
())
{
try
{
try
{
run
.
getCode
().
run
();
run
.
getCode
().
run
();
...
@@ -235,7 +228,6 @@ public class ThreadHelper {
...
@@ -235,7 +228,6 @@ public class ThreadHelper {
}
}
sleep
(
run
.
getTime
());
sleep
(
run
.
getTime
());
}
}
}
});
});
}
}
...
...
yzg-util-base/src/test/java/helper/TestTimeout.java
View file @
8d3d3c20
...
@@ -19,4 +19,11 @@ public class TestTimeout {
...
@@ -19,4 +19,11 @@ public class TestTimeout {
// System.err.println("已经等待" + time);
// System.err.println("已经等待" + time);
});
});
}
}
@Test
public
void
testQueue
()
{
for
(
int
i
=
0
;
i
<
50000
;
i
++)
{
ThreadHelper
.
runThread
(()
->
test
());
}
}
}
}
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