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
09c59889
Commit
09c59889
authored
Mar 21, 2019
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
线程相关处理
parent
5ee7bdd0
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
631 additions
and
427 deletions
+631
-427
ExceptionHelper.java
...ain/java/com/yanzuoguang/util/helper/ExceptionHelper.java
+13
-1
RunHelper.java
src/main/java/com/yanzuoguang/util/helper/RunHelper.java
+1
-0
ThreadHelper.java
src/main/java/com/yanzuoguang/util/helper/ThreadHelper.java
+0
-426
RunExecute.java
...n/java/com/yanzuoguang/util/helper/thread/RunExecute.java
+18
-0
RunInterval.java
.../java/com/yanzuoguang/util/helper/thread/RunInterval.java
+55
-0
RunPlan.java
...main/java/com/yanzuoguang/util/helper/thread/RunPlan.java
+205
-0
RunPlanData.java
.../java/com/yanzuoguang/util/helper/thread/RunPlanData.java
+110
-0
ThreadHelper.java
...java/com/yanzuoguang/util/helper/thread/ThreadHelper.java
+229
-0
No files found.
src/main/java/com/yanzuoguang/util/helper/ExceptionHelper.java
View file @
09c59889
...
@@ -7,10 +7,22 @@ public class ExceptionHelper {
...
@@ -7,10 +7,22 @@ public class ExceptionHelper {
/**
/**
* 不跑出异常,记录日常日志
* 不跑出异常,记录日常日志
*
*
* @param cls
* @param ex
*/
public
static
void
handleException
(
Class
<?>
cls
,
Throwable
ex
)
{
handleException
(
cls
,
ex
,
null
);
}
/**
* 不跑出异常,记录日常日志
*
* @param cls
* @param ex
* @param ex
* @param from
* @param from
*/
*/
public
static
void
handleException
(
Throwable
ex
,
Object
from
)
{
public
static
void
handleException
(
Class
<?>
cls
,
Throwable
ex
,
Object
from
)
{
ex
.
printStackTrace
();
// todo: 需要修改
// todo: 需要修改
// Log.error(ExceptionHelper.class, ex, StringHelper.toString(from));
// Log.error(ExceptionHelper.class, ex, StringHelper.toString(from));
}
}
...
...
src/main/java/com/yanzuoguang/util/helper/RunHelper.java
View file @
09c59889
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.util.helper;
...
@@ -2,6 +2,7 @@ package com.yanzuoguang.util.helper;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.exception.CodeException
;
import
com.yanzuoguang.util.helper.thread.ThreadHelper
;
/**
/**
* 重复执行工具类
* 重复执行工具类
...
...
src/main/java/com/yanzuoguang/util/helper/ThreadHelper.java
deleted
100644 → 0
View file @
5ee7bdd0
This diff is collapsed.
Click to expand it.
src/main/java/com/yanzuoguang/util/helper/thread/RunExecute.java
0 → 100644
View file @
09c59889
package
com
.
yanzuoguang
.
util
.
helper
.
thread
;
import
com.yanzuoguang.util.helper.EventRun
;
/**
* Runnable时间触发处理器
*/
public
class
RunExecute
implements
EventRun
<
Runnable
>
{
/**
* 执行时间
*
* @param t 接口 需要执行的对象
* @throws Exception 跑出异常
*/
public
void
Execute
(
Runnable
t
)
{
t
.
run
();
}
}
src/main/java/com/yanzuoguang/util/helper/thread/RunInterval.java
0 → 100644
View file @
09c59889
package
com
.
yanzuoguang
.
util
.
helper
.
thread
;
/**
* 循环执行任务
*/
public
class
RunInterval
{
/**
* 当前线程
*/
private
Thread
Thread
;
/**
* 间隔时间,毫秒
*/
private
int
Time
;
/**
* 执行的代码
*/
private
Runnable
Code
;
/**
* 是否终端
*/
private
boolean
Break
;
public
java
.
lang
.
Thread
getThread
()
{
return
Thread
;
}
public
void
setThread
(
java
.
lang
.
Thread
thread
)
{
Thread
=
thread
;
}
public
int
getTime
()
{
return
Time
;
}
public
void
setTime
(
int
time
)
{
Time
=
time
;
}
public
Runnable
getCode
()
{
return
Code
;
}
public
void
setCode
(
Runnable
code
)
{
Code
=
code
;
}
public
boolean
isBreak
()
{
return
Break
;
}
public
void
setBreak
(
boolean
aBreak
)
{
Break
=
aBreak
;
}
}
\ No newline at end of file
src/main/java/com/yanzuoguang/util/helper/thread/RunPlan.java
0 → 100644
View file @
09c59889
package
com
.
yanzuoguang
.
util
.
helper
.
thread
;
import
com.yanzuoguang.util.helper.Event
;
import
com.yanzuoguang.util.helper.ExceptionHelper
;
import
com.yanzuoguang.util.helper.StringHelper
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* 自动执行任务
*/
public
class
RunPlan
{
// 任务锁
private
Object
m_Lock
=
new
Object
();
// 任务队列
private
List
<
RunPlanData
>
m_List
=
new
ArrayList
<>();
// 增加时触发的事件队列
private
Event
<
Runnable
>
onAdd
=
new
Event
<>();
// 增加时触发的事件队列
private
Event
<
Runnable
>
onRemove
=
new
Event
<>();
// 单项成功执行时的事件队列
private
Event
<
Runnable
>
onItemExecuted
=
new
Event
<>();
// 全部执行完时触发的事件队列
private
Event
<
Runnable
>
onExecuted
=
new
Event
<>();
/**
* 增加时触发的事件队列
*
* @return 事件队列
*/
public
Event
<
Runnable
>
getOnAdd
()
{
return
onAdd
;
}
/**
* 增加时触发的事件队列
*
* @return 事件队列
*/
public
Event
<
Runnable
>
getOnRemove
()
{
return
onRemove
;
}
/**
* 单项成功执行时的事件队列
*
* @return 事件队列
*/
public
Event
<
Runnable
>
getOnItemExecuted
()
{
return
onItemExecuted
;
}
/**
* 全部执行完时触发的事件队列
*
* @return 事件队列
*/
public
Event
<
Runnable
>
getOnExecuted
()
{
return
onExecuted
;
}
/**
* 执行完任务后,是否需要删除该任务,并且设置允许执行的最大错误
*
* @param isRemove 执行完成是否删除
* @param maxError 最大错误次数
*/
public
final
void
Execute
(
boolean
isRemove
,
int
maxError
)
{
for
(
int
i
=
m_List
.
size
()
-
1
;
i
>=
0
;
i
--)
{
Date
now
=
new
Date
();
RunPlanData
item
;
synchronized
(
this
.
m_Lock
)
{
item
=
m_List
.
size
()
>
i
?
m_List
.
get
(
i
)
:
null
;
}
if
(
item
==
null
)
{
continue
;
}
double
millSeconds
=
0
;
// 在Window CE中时间相减可能会出错
try
{
// 处理非法改动时间
if
(
item
.
getDate
().
compareTo
(
now
)
>
0
)
{
item
.
setDate
(
now
);
}
millSeconds
=
(
now
.
getTime
()
-
item
.
getDate
().
getTime
());
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
// 未到执行时间
if
(
millSeconds
<=
item
.
getTime
())
{
continue
;
}
if
(
maxError
==
-
1
||
item
.
getExecuteError
()
<
maxError
)
{
try
{
item
.
addCount
();
item
.
getExecute
().
run
();
item
.
initError
();
this
.
triggerEvent
(
this
.
onItemExecuted
);
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
RunPlan
.
class
,
ex
);
}
item
.
initDate
();
}
try
{
if
(
isRemove
)
{
synchronized
(
this
.
m_Lock
)
{
this
.
m_List
.
remove
(
i
);
this
.
triggerEvent
(
this
.
onRemove
);
}
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
RunPlan
.
class
,
ex
);
}
}
this
.
triggerEvent
(
this
.
onExecuted
);
}
/**
* 设置需要执行的任务
*
* @param run 需要执行的程序
* @param vTime 间隔时间
* @return 执行标志
*/
public
final
String
Set
(
Runnable
run
,
int
vTime
)
{
// 创建临时任务数据
RunPlanData
temp
=
new
RunPlanData
();
temp
.
setFlag
(
StringHelper
.
getNewID
());
temp
.
setExecute
(
run
);
temp
.
setTime
(
vTime
);
synchronized
(
m_Lock
)
{
m_List
.
add
(
temp
);
}
this
.
triggerEvent
(
this
.
onAdd
);
return
temp
.
getFlag
();
}
/**
* 清除需要执行的任务
*
* @param flag 执行标志
*/
public
final
void
remove
(
String
flag
)
{
synchronized
(
m_Lock
)
{
for
(
RunPlanData
item
:
this
.
m_List
)
{
if
(
item
.
getFlag
().
equals
(
flag
))
{
this
.
m_List
.
remove
(
item
);
this
.
triggerEvent
(
this
.
onRemove
);
break
;
}
}
}
}
/**
* 清除需要执行的任务
*
* @param run 执行的方法,注意有多个同样的方法时,只会清除第一个
*/
public
final
void
remove
(
Runnable
run
)
{
synchronized
(
m_Lock
)
{
for
(
RunPlanData
item
:
this
.
m_List
)
{
if
(
item
.
getExecute
().
equals
(
run
))
{
this
.
m_List
.
remove
(
item
);
this
.
triggerEvent
(
this
.
onRemove
);
break
;
}
}
}
}
/**
* 触发事件
*
* @param run 需要触发的事件队列
*/
private
void
triggerEvent
(
Event
<
Runnable
>
run
)
{
try
{
if
(
run
!=
null
)
{
run
.
exeucte
(
new
RunExecute
());
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
}
/**
* 获取需要执行的任务长度
*
* @return 长度信息
*/
public
final
int
getCount
()
{
return
this
.
m_List
.
size
();
}
}
src/main/java/com/yanzuoguang/util/helper/thread/RunPlanData.java
0 → 100644
View file @
09c59889
package
com
.
yanzuoguang
.
util
.
helper
.
thread
;
import
java.util.Date
;
/***
* 任务项数据
*/
public
class
RunPlanData
{
/**
* 任务开始时间
*/
private
Date
date
;
/**
* 执行标记
*/
private
String
flag
;
/**
* 执行次数
*/
private
double
executeCount
;
/**
* 执行时错误次数
*/
private
double
executeError
;
// 调试变量
/**
* 执行时间
*/
private
int
time
;
/**
* 需要执行的对象
*/
private
Runnable
execute
;
/**
* 构造函数
*/
public
RunPlanData
()
{
this
.
initDate
();
}
public
Date
getDate
()
{
return
date
;
}
public
void
setDate
(
Date
date
)
{
this
.
date
=
date
;
}
public
String
getFlag
()
{
return
flag
;
}
public
void
setFlag
(
String
flag
)
{
this
.
flag
=
flag
;
}
public
double
getExecuteCount
()
{
return
executeCount
;
}
public
void
setExecuteCount
(
double
executeCount
)
{
this
.
executeCount
=
executeCount
;
}
public
double
getExecuteError
()
{
return
executeError
;
}
public
void
setExecuteError
(
double
executeError
)
{
this
.
executeError
=
executeError
;
}
public
int
getTime
()
{
return
time
;
}
public
void
setTime
(
int
time
)
{
this
.
time
=
time
;
}
public
Runnable
getExecute
()
{
return
execute
;
}
public
void
setExecute
(
Runnable
execute
)
{
this
.
execute
=
execute
;
}
/**
* 添加执行次数
*/
public
void
addCount
()
{
this
.
executeCount
++;
this
.
executeError
++;
}
/**
* 添加执行次数
*/
public
void
initError
()
{
this
.
executeError
=
0
;
}
/**
* 重置时间
*/
public
void
initDate
()
{
this
.
date
=
new
Date
();
}
}
src/main/java/com/yanzuoguang/util/helper/thread/ThreadHelper.java
0 → 100644
View file @
09c59889
package
com
.
yanzuoguang
.
util
.
helper
.
thread
;
import
com.yanzuoguang.util.helper.DateHelper
;
import
com.yanzuoguang.util.helper.ExceptionHelper
;
import
java.util.Date
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
/**
* 线程帮助类
*/
public
class
ThreadHelper
{
private
static
Object
threadLock
=
new
Object
();
private
static
boolean
threadIsRun
=
false
;
private
static
Date
threadDate
=
null
;
private
static
RunPlan
timeout
;
private
static
RunPlan
interval
;
// 线程对象
private
static
ExecutorService
_ExecuteService
=
Executors
.
newCachedThreadPool
();
/**
* 初始化线程对象,需要调用该函数之后才能使用
*/
public
static
void
init
()
{
if
(
timeout
!=
null
||
interval
!=
null
)
{
return
;
}
threadDate
=
DateHelper
.
getDateTime
(
"1987-11-04"
);
timeout
=
new
RunPlan
();
interval
=
new
RunPlan
();
Runnable
addExecute
=
new
Runnable
()
{
public
void
run
()
{
OnExecuteAdd
();
}
};
Runnable
itemExecuted
=
new
Runnable
()
{
public
void
run
()
{
OnItemExecuted
();
}
};
timeout
.
getOnAdd
().
add
(
addExecute
);
timeout
.
getOnItemExecuted
().
add
(
itemExecuted
);
interval
.
getOnAdd
().
add
(
addExecute
);
interval
.
getOnItemExecuted
().
add
(
itemExecuted
);
StartMonitor
();
}
private
static
void
OnItemExecuted
()
{
threadDate
=
new
Date
();
}
private
static
void
OnExecuteAdd
()
{
startThread
();
}
private
static
void
startThread
()
{
synchronized
(
threadLock
)
{
if
(
threadIsRun
)
{
return
;
}
runThread
(
new
Runnable
()
{
public
void
run
()
{
threadIsRun
=
true
;
while
(
threadIsRun
)
{
try
{
timeout
.
Execute
(
true
,
-
1
);
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
try
{
interval
.
Execute
(
false
,
-
1
);
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
if
(
timeout
.
getCount
()
==
0
&&
interval
.
getCount
()
==
0
)
{
break
;
}
sleep
(
10
);
}
threadIsRun
=
false
;
}
});
}
}
/**
* 监控线程的方法,防止线程执行死机
*/
private
static
void
StartMonitor
()
{
runThread
(
new
Runnable
()
{
public
void
run
()
{
do
{
if
(
threadIsRun
&&
((
new
Date
().
getTime
()
-
threadDate
.
getTime
())
/
1000
)
>
5
)
{
try
{
if
(
threadIsRun
)
{
threadIsRun
=
false
;
}
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
threadIsRun
=
false
;
startThread
();
}
sleep
(
1000
*
60
);
}
while
(
true
);
}
});
}
/**
* 多少毫秒后执行任务,适用于执行时间短,不需要单独开线程的程序
*
* @param run 需要执行的程序
* @param vTime 间隔时间
* @return 执行标志
*/
public
static
String
setTimeout
(
Runnable
run
,
int
vTime
)
{
return
timeout
.
Set
(
run
,
vTime
);
}
/**
* 清除需要执行的任务
*
* @param flag 执行标志
*/
public
static
void
clearTimeout
(
String
flag
)
{
timeout
.
remove
(
flag
);
}
/**
* 清除需要执行的任务
*
* @param run 执行的方法,注意有多个同样的方法时,只会清除第一个
*/
public
static
void
clearTimeout
(
Runnable
run
)
{
timeout
.
remove
(
run
);
}
/**
* 每隔多少毫秒执行任务,适用于执行时间短,不需要单独开线程的程序
*
* @param run 需要执行的程序
* @param vTime 间隔时间
* @return 执行标志
*/
public
static
String
setInterval
(
Runnable
run
,
int
vTime
)
{
return
interval
.
Set
(
run
,
vTime
);
}
/**
* 清除需要执行的任务
*
* @param flag 执行标志
*/
public
static
void
clearInterval
(
String
flag
)
{
interval
.
remove
(
flag
);
}
/**
* 清除需要执行的任务
*
* @param run 执行的方法,注意有多个同样的方法时,只会清除第一个
*/
public
static
void
clearInterval
(
Runnable
run
)
{
interval
.
remove
(
run
);
}
/**
* 沉睡
*
* @param mills 时间(毫秒)
*/
public
static
void
sleep
(
int
mills
)
{
try
{
Thread
.
sleep
(
mills
);
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
,
mills
);
}
}
/**
* 执行对象并且处理异常
*
* @param run 需要执行的对象
*/
public
static
void
executeCatch
(
Runnable
run
)
{
try
{
run
.
run
();
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
,
null
);
}
}
/**
* 需要执行的线程
*
* @param run
*/
public
static
void
runThread
(
final
Runnable
run
)
{
_ExecuteService
.
execute
(
new
Runnable
()
{
@Override
public
void
run
()
{
executeCatch
(
run
);
}
});
}
/**
* 循环执行事务,直到停止执行
*
* @param run
*/
public
static
void
runThread
(
final
RunInterval
run
)
{
runThread
(
new
Runnable
()
{
public
void
run
()
{
while
(!
run
.
isBreak
())
{
try
{
run
.
getCode
().
run
();
}
catch
(
Exception
ex
)
{
ExceptionHelper
.
handleException
(
ThreadHelper
.
class
,
ex
);
}
sleep
(
run
.
getTime
());
}
}
});
}
}
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