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
9edb0efa
Commit
9edb0efa
authored
Sep 19, 2022
by
yanzg
Browse files
Options
Browse Files
Download
Plain Diff
Merge branches 'master' and 'ver1.1' of
http://git.tbd.yanzuoguang.com/yzg/yzg-util
parents
8cf743df
552c2c68
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
144 additions
and
26 deletions
+144
-26
YzgTimeout.java
...src/main/java/com/yanzuoguang/util/helper/YzgTimeout.java
+122
-26
TestTimeout.java
yzg-util-base/src/test/java/helper/TestTimeout.java
+22
-0
No files found.
yzg-util-base/src/main/java/com/yanzuoguang/util/helper/YzgTimeout.java
View file @
9edb0efa
...
@@ -2,8 +2,12 @@ package com.yanzuoguang.util.helper;
...
@@ -2,8 +2,12 @@ package com.yanzuoguang.util.helper;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.log.Log
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
com.yanzuoguang.util.vo.Ref
;
import
java.util.Queue
;
import
java.util.concurrent.ArrayBlockingQueue
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.ThreadPoolExecutor
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Consumer
;
import
java.util.function.Consumer
;
/**
/**
...
@@ -15,7 +19,9 @@ public class YzgTimeout {
...
@@ -15,7 +19,9 @@ public class YzgTimeout {
public
static
final
int
TIME_OUT_DEFAULT
=
15
*
1000
;
public
static
final
int
TIME_OUT_DEFAULT
=
15
*
1000
;
public
static
final
int
TIME_OUT_TIP
=
10
*
1000
;
public
static
final
int
TIME_OUT_TIP
=
10
*
1000
;
public
static
final
int
TIME_OUT_UNIT
=
10
;
private
static
ExecutorService
executorService
;
private
static
final
Queue
<
TimeInfo
>
queueInfos
=
new
ArrayBlockingQueue
<>(
500
);
/**
/**
* 超时监控
* 超时监控
...
@@ -36,7 +42,7 @@ public class YzgTimeout {
...
@@ -36,7 +42,7 @@ public class YzgTimeout {
* @param runnable 运行函数
* @param runnable 运行函数
*/
*/
public
static
void
timeOut
(
Class
<?>
cls
,
String
message
,
Runnable
runnable
,
Consumer
<
Long
>
consumer
)
{
public
static
void
timeOut
(
Class
<?>
cls
,
String
message
,
Runnable
runnable
,
Consumer
<
Long
>
consumer
)
{
timeHeart
(
TIME_OUT_DEFAULT
,
TIME_OUT_
UNIT
,
TIME_OUT_
TIP
,
runnable
,
(
time
)
->
{
timeHeart
(
TIME_OUT_DEFAULT
,
TIME_OUT_TIP
,
runnable
,
(
time
)
->
{
Log
.
error
(
cls
,
"%s超时,已经执行%d豪秒,正在等待执行完成"
,
message
,
time
);
Log
.
error
(
cls
,
"%s超时,已经执行%d豪秒,正在等待执行完成"
,
message
,
time
);
if
(
consumer
!=
null
)
{
if
(
consumer
!=
null
)
{
consumer
.
accept
(
time
);
consumer
.
accept
(
time
);
...
@@ -51,7 +57,7 @@ public class YzgTimeout {
...
@@ -51,7 +57,7 @@ public class YzgTimeout {
* @param heart 心跳函数
* @param heart 心跳函数
*/
*/
public
static
void
timeHeart
(
Runnable
runnable
,
YzgTimeoutHeart
heart
)
{
public
static
void
timeHeart
(
Runnable
runnable
,
YzgTimeoutHeart
heart
)
{
timeHeart
(
1000
,
1000
,
10
,
runnable
,
heart
);
timeHeart
(
TIME_OUT_DEFAULT
,
TIME_OUT_TIP
,
runnable
,
heart
);
}
}
/**
/**
...
@@ -59,36 +65,126 @@ public class YzgTimeout {
...
@@ -59,36 +65,126 @@ public class YzgTimeout {
*
*
* @param tipOutDefault 默认超时时间
* @param tipOutDefault 默认超时时间
* @param timeOutTip 超时心跳间隔
* @param timeOutTip 超时心跳间隔
* @param tipUnit 监听间隔时间(监听任务完成间隔时间)
* @param runnable 运行函数
* @param runnable 运行函数
* @param heart 心跳函数
* @param heart 心跳函数
*/
*/
public
static
void
timeHeart
(
int
tipOutDefault
,
int
timeOutTip
,
int
tipUnit
,
public
static
void
timeHeart
(
int
tipOutDefault
,
int
timeOutTip
,
Runnable
runnable
,
YzgTimeoutHeart
heart
)
{
Runnable
runnable
,
YzgTimeoutHeart
heart
)
{
final
Ref
<
Boolean
>
isRun
=
new
Ref
<>(
false
);
TimeInfo
timeInfo
=
getTimeInfo
(
tipOutDefault
,
timeOutTip
,
heart
);
ThreadHelper
.
runThread
(()
->
{
try
{
long
timeMax
=
tipOutDefault
;
long
start
=
System
.
currentTimeMillis
();
do
{
long
end
=
System
.
currentTimeMillis
();
long
time
=
end
-
start
;
if
(
time
>
timeMax
)
{
timeMax
+=
timeOutTip
;
heart
.
heart
(
time
);
}
ThreadHelper
.
sleep
(
tipUnit
);
}
while
(!
isRun
.
value
);
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
});
try
{
try
{
runnable
.
run
();
runnable
.
run
();
}
finally
{
}
finally
{
synchronized
(
isRun
)
{
synchronized
(
timeInfo
)
{
isRun
.
value
=
true
;
timeInfo
.
setRun
(
true
);
}
}
}
private
static
TimeInfo
getTimeInfo
(
int
tipOutDefault
,
int
timeOutTip
,
YzgTimeoutHeart
heart
)
{
init
();
TimeInfo
timeInfo
=
new
TimeInfo
(
tipOutDefault
,
timeOutTip
,
heart
);
queueInfos
.
add
(
timeInfo
);
return
timeInfo
;
}
private
static
void
init
()
{
synchronized
(
YzgTimeout
.
class
)
{
if
(
executorService
==
null
)
{
executorService
=
new
ThreadPoolExecutor
(
1
,
10
,
1000
,
TimeUnit
.
MILLISECONDS
,
new
ArrayBlockingQueue
<>(
10
));
}
}
ThreadHelper
.
runThread
(()
->
{
while
(
true
)
{
runItem
();
ThreadHelper
.
sleep
(
1000
);
}
});
}
private
static
void
runItem
()
{
int
size
=
queueInfos
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
TimeInfo
poll
=
queueInfos
.
poll
();
long
end
=
System
.
currentTimeMillis
();
long
time
=
end
-
poll
.
getStart
();
if
(
time
>
poll
.
getTimeMax
())
{
try
{
poll
.
setTimeMax
(
poll
.
getTimeMax
()
+
poll
.
getTimeOutTip
());
executorService
.
submit
(()
->
poll
.
getHeart
().
heart
(
time
));
}
catch
(
Exception
ex
)
{
ex
.
printStackTrace
();
}
}
if
(!
poll
.
isRun
())
{
queueInfos
.
add
(
poll
);
}
}
}
}
}
}
}
}
class
TimeInfo
{
private
int
timeOutDefault
;
private
int
timeOutTip
;
private
YzgTimeoutHeart
heart
;
private
boolean
run
;
private
long
start
;
private
int
timeMax
;
public
TimeInfo
(
int
timeOutDefault
,
int
timeOutTip
,
YzgTimeoutHeart
heart
)
{
this
.
timeOutDefault
=
timeOutDefault
;
this
.
timeOutTip
=
timeOutTip
;
this
.
heart
=
heart
;
this
.
timeMax
=
timeOutDefault
;
this
.
start
=
System
.
currentTimeMillis
();
}
public
int
getTimeOutDefault
()
{
return
timeOutDefault
;
}
public
void
setTimeOutDefault
(
int
timeOutDefault
)
{
this
.
timeOutDefault
=
timeOutDefault
;
}
public
int
getTimeOutTip
()
{
return
timeOutTip
;
}
public
void
setTimeOutTip
(
int
timeOutTip
)
{
this
.
timeOutTip
=
timeOutTip
;
}
public
YzgTimeoutHeart
getHeart
()
{
return
heart
;
}
public
void
setHeart
(
YzgTimeoutHeart
heart
)
{
this
.
heart
=
heart
;
}
public
boolean
isRun
()
{
return
run
;
}
public
void
setRun
(
boolean
run
)
{
this
.
run
=
run
;
}
public
long
getStart
()
{
return
start
;
}
public
void
setStart
(
long
start
)
{
this
.
start
=
start
;
}
public
int
getTimeMax
()
{
return
timeMax
;
}
public
void
setTimeMax
(
int
timeMax
)
{
this
.
timeMax
=
timeMax
;
}
}
yzg-util-base/src/test/java/helper/TestTimeout.java
0 → 100644
View file @
9edb0efa
package
helper
;
import
com.yanzuoguang.util.helper.YzgTimeout
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
org.junit.Test
;
public
class
TestTimeout
{
@Test
public
void
test
()
{
YzgTimeout
.
timeOut
(
TestTimeout
.
class
,
"消息"
,
new
Runnable
()
{
@Override
public
void
run
()
{
System
.
out
.
println
(
"开始运行"
);
ThreadHelper
.
sleep
(
1
*
60
*
1000
);
System
.
out
.
println
(
"结束运行"
);
}
},
(
time
)
->
{
// System.err.println("已经等待" + time);
});
}
}
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