Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
QZero_0
java-design-patterns
提交
fd8c0584
J
java-design-patterns
项目概览
QZero_0
/
java-design-patterns
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
java-design-patterns
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fd8c0584
编写于
12月 30, 2015
作者:
J
Jeroen Meulemeester
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added tests for thread-pool pattern
Fixed concurrency problem in id generation of Task
上级
47709e24
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
198 addition
and
5 deletion
+198
-5
thread-pool/pom.xml
thread-pool/pom.xml
+5
-0
thread-pool/src/main/java/com/iluwatar/threadpool/CoffeeMakingTask.java
...c/main/java/com/iluwatar/threadpool/CoffeeMakingTask.java
+1
-1
thread-pool/src/main/java/com/iluwatar/threadpool/PotatoPeelingTask.java
.../main/java/com/iluwatar/threadpool/PotatoPeelingTask.java
+1
-1
thread-pool/src/main/java/com/iluwatar/threadpool/Task.java
thread-pool/src/main/java/com/iluwatar/threadpool/Task.java
+5
-3
thread-pool/src/test/java/com/iluwatar/threadpool/CoffeeMakingTaskTest.java
...st/java/com/iluwatar/threadpool/CoffeeMakingTaskTest.java
+17
-0
thread-pool/src/test/java/com/iluwatar/threadpool/PotatoPeelingTaskTest.java
...t/java/com/iluwatar/threadpool/PotatoPeelingTaskTest.java
+17
-0
thread-pool/src/test/java/com/iluwatar/threadpool/TaskTest.java
...-pool/src/test/java/com/iluwatar/threadpool/TaskTest.java
+121
-0
thread-pool/src/test/java/com/iluwatar/threadpool/WorkerTest.java
...ool/src/test/java/com/iluwatar/threadpool/WorkerTest.java
+31
-0
未找到文件。
thread-pool/pom.xml
浏览文件 @
fd8c0584
...
...
@@ -14,5 +14,10 @@
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-core
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
thread-pool/src/main/java/com/iluwatar/threadpool/CoffeeMakingTask.java
浏览文件 @
fd8c0584
...
...
@@ -7,7 +7,7 @@ package com.iluwatar.threadpool;
*/
public
class
CoffeeMakingTask
extends
Task
{
private
static
final
int
TIME_PER_CUP
=
3
00
;
private
static
final
int
TIME_PER_CUP
=
1
00
;
public
CoffeeMakingTask
(
int
numCups
)
{
super
(
numCups
*
TIME_PER_CUP
);
...
...
thread-pool/src/main/java/com/iluwatar/threadpool/PotatoPeelingTask.java
浏览文件 @
fd8c0584
...
...
@@ -7,7 +7,7 @@ package com.iluwatar.threadpool;
*/
public
class
PotatoPeelingTask
extends
Task
{
private
static
final
int
TIME_PER_POTATO
=
5
00
;
private
static
final
int
TIME_PER_POTATO
=
2
00
;
public
PotatoPeelingTask
(
int
numPotatoes
)
{
super
(
numPotatoes
*
TIME_PER_POTATO
);
...
...
thread-pool/src/main/java/com/iluwatar/threadpool/Task.java
浏览文件 @
fd8c0584
package
com.iluwatar.threadpool
;
import
java.util.concurrent.atomic.AtomicInteger
;
/**
*
*
* Abstract base class for tasks
*
*/
public
abstract
class
Task
{
private
static
int
nextId
=
1
;
private
static
final
AtomicInteger
ID_GENERATOR
=
new
AtomicInteger
()
;
private
final
int
id
;
private
final
int
timeMs
;
public
Task
(
final
int
timeMs
)
{
this
.
id
=
nextId
++
;
this
.
id
=
ID_GENERATOR
.
incrementAndGet
()
;
this
.
timeMs
=
timeMs
;
}
...
...
thread-pool/src/test/java/com/iluwatar/threadpool/CoffeeMakingTaskTest.java
0 → 100644
浏览文件 @
fd8c0584
package
com.iluwatar.threadpool
;
/**
* Date: 12/30/15 - 18:23 PM
*
* @author Jeroen Meulemeester
*/
public
class
CoffeeMakingTaskTest
extends
TaskTest
<
CoffeeMakingTask
>
{
/**
* Create a new test instance
*/
public
CoffeeMakingTaskTest
()
{
super
(
CoffeeMakingTask:
:
new
,
100
);
}
}
thread-pool/src/test/java/com/iluwatar/threadpool/PotatoPeelingTaskTest.java
0 → 100644
浏览文件 @
fd8c0584
package
com.iluwatar.threadpool
;
/**
* Date: 12/30/15 - 18:23 PM
*
* @author Jeroen Meulemeester
*/
public
class
PotatoPeelingTaskTest
extends
TaskTest
<
PotatoPeelingTask
>
{
/**
* Create a new test instance
*/
public
PotatoPeelingTaskTest
()
{
super
(
PotatoPeelingTask:
:
new
,
200
);
}
}
\ No newline at end of file
thread-pool/src/test/java/com/iluwatar/threadpool/TaskTest.java
0 → 100644
浏览文件 @
fd8c0584
package
com.iluwatar.threadpool
;
import
org.junit.Test
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
/**
* Date: 12/30/15 - 18:22 PM
*
* @author Jeroen Meulemeester
*/
public
abstract
class
TaskTest
<
T
extends
Task
>
{
/**
* The number of tasks used during the concurrency test
*/
private
static
final
int
TASK_COUNT
=
128
*
1024
;
/**
* The number of threads used during the concurrency test
*/
private
static
final
int
THREAD_COUNT
=
8
;
/**
* The task factory, used to create new test items
*/
private
final
Function
<
Integer
,
T
>
factory
;
/**
* The expected time needed to run the task 1 single time, in milli seconds
*/
private
final
int
expectedExecutionTime
;
/**
* Create a new test instance
*
* @param factory The task factory, used to create new test items
* @param expectedExecutionTime The expected time needed to run the task 1 time, in milli seconds
*/
public
TaskTest
(
final
Function
<
Integer
,
T
>
factory
,
final
int
expectedExecutionTime
)
{
this
.
factory
=
factory
;
this
.
expectedExecutionTime
=
expectedExecutionTime
;
}
/**
* Verify if the generated id is unique for each task, even if the tasks are created in separate
* threads
*/
@Test
(
timeout
=
10000
)
public
void
testIdGeneration
()
throws
Exception
{
final
ExecutorService
service
=
Executors
.
newFixedThreadPool
(
THREAD_COUNT
);
final
List
<
Callable
<
Integer
>>
tasks
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
TASK_COUNT
;
i
++)
{
tasks
.
add
(()
->
factory
.
apply
(
1
).
getId
());
}
final
List
<
Integer
>
ids
=
service
.
invokeAll
(
tasks
)
.
stream
()
.
map
(
TaskTest:
:
get
)
.
filter
(
Objects:
:
nonNull
)
.
collect
(
Collectors
.
toList
());
service
.
shutdownNow
();
final
long
uniqueIdCount
=
ids
.
stream
()
.
distinct
()
.
count
();
assertEquals
(
TASK_COUNT
,
ids
.
size
());
assertEquals
(
TASK_COUNT
,
uniqueIdCount
);
}
/**
* Verify if the time per execution of a task matches the actual time required to execute the task
* a given number of times
*/
@Test
public
void
testTimeMs
()
{
for
(
int
i
=
0
;
i
<
10
;
i
++)
{
assertEquals
(
this
.
expectedExecutionTime
*
i
,
this
.
factory
.
apply
(
i
).
getTimeMs
());
}
}
/**
* Verify if the task has some sort of {@link T#toString()}, different from 'null'
*/
@Test
public
void
testToString
()
{
assertNotNull
(
this
.
factory
.
apply
(
0
).
toString
());
}
/**
* Extract the result from a future or returns 'null' when an exception occurred
*
* @param future The future we want the result from
* @param <O> The result type
* @return The result or 'null' when a checked exception occurred
*/
private
static
<
O
>
O
get
(
Future
<
O
>
future
)
{
try
{
return
future
.
get
();
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
return
null
;
}
}
}
\ No newline at end of file
thread-pool/src/test/java/com/iluwatar/threadpool/WorkerTest.java
0 → 100644
浏览文件 @
fd8c0584
package
com.iluwatar.threadpool
;
import
org.junit.Test
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
verify
;
import
static
org
.
mockito
.
Mockito
.
verifyNoMoreInteractions
;
import
static
org
.
mockito
.
Mockito
.
verifyZeroInteractions
;
/**
* Date: 12/30/15 - 18:21 PM
*
* @author Jeroen Meulemeester
*/
public
class
WorkerTest
{
/**
* Verify if a worker does the actual job
*/
@Test
public
void
testRun
()
{
final
Task
task
=
mock
(
Task
.
class
);
final
Worker
worker
=
new
Worker
(
task
);
verifyZeroInteractions
(
task
);
worker
.
run
();
verify
(
task
).
getTimeMs
();
verifyNoMoreInteractions
(
task
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录