Added tests for thread-pool pattern
Fixed concurrency problem in id generation of Task
This commit is contained in:
@ -7,7 +7,7 @@ package com.iluwatar.threadpool;
|
||||
*/
|
||||
public class CoffeeMakingTask extends Task {
|
||||
|
||||
private static final int TIME_PER_CUP = 300;
|
||||
private static final int TIME_PER_CUP = 100;
|
||||
|
||||
public CoffeeMakingTask(int numCups) {
|
||||
super(numCups * TIME_PER_CUP);
|
||||
|
@ -7,7 +7,7 @@ package com.iluwatar.threadpool;
|
||||
*/
|
||||
public class PotatoPeelingTask extends Task {
|
||||
|
||||
private static final int TIME_PER_POTATO = 500;
|
||||
private static final int TIME_PER_POTATO = 200;
|
||||
|
||||
public PotatoPeelingTask(int numPotatoes) {
|
||||
super(numPotatoes * TIME_PER_POTATO);
|
||||
|
@ -1,19 +1,21 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user