Skip to content

Instantly share code, notes, and snippets.

@FaAway
Created March 19, 2016 12:54
Show Gist options
  • Save FaAway/bf58271ba15bc5639c1a to your computer and use it in GitHub Desktop.
Save FaAway/bf58271ba15bc5639c1a to your computer and use it in GitHub Desktop.
javarush level29.lesson09.bonus01
package com.javarush.test.level29.lesson09.bonus01;
import java.util.concurrent.*;
/* Argument and Value are generic types*/
public class CacheComputeManager<Argument, Value> implements Computable<Argument, Value> {
private final ConcurrentHashMap<Argument, Future<Value>> cache = new ConcurrentHashMap<>();
private final Computable<Argument, Value> computable;
private final ExecutorService service = Executors.newFixedThreadPool(5);
public CacheComputeManager(Computable<Argument, Value> computable) {
this.computable = computable;
}
@Override
public Value compute(final Argument arg) throws InterruptedException {
Future<Value> f = cache.get(arg);
if (f == null) {
FutureTask<Value> ft = createFutureTaskForNewArgumentThatHaveToComputeValue(arg);
cache.putIfAbsent(arg, ft);
f = ft;
ft.run();
System.out.print(arg + " will be cached ");
} else {
System.out.print(arg + " taken from cache ");
}
try {
return f.get();
} catch (CancellationException e) {
cache.remove(arg, f);
} catch (ExecutionException e) {
throw new RuntimeException(e.getCause());
}
return null;
}
public FutureTask<Value> createFutureTaskForNewArgumentThatHaveToComputeValue(final Argument arg) {
return new FutureTask<Value>(new Callable<Value>() {
@Override
public Value call() throws Exception {
return computable.compute(arg);
}
});
}
}
package com.javarush.test.level29.lesson09.bonus01;
/* Argument and Value are generic types*/
public interface Computable<Argument, Value> {
Value compute(Argument argument) throws InterruptedException;
}
package com.javarush.test.level29.lesson09.bonus01;
/* Кеширование
В CacheComputeManager реализуйте логику пустого метода.
Догадайтесь, что он должен делать по названию метода и по логике класса.
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
//////////////first example///////////////////
Square square = new Square();
CacheComputeManager<Integer, Integer> manager = new CacheComputeManager(square);
for (int i = 0; i < 8; i++) {
int j = i % 4;
Integer result = manager.compute(j);
System.out.format("%d * %d = %d\n", j, j, result);
}
/* output
0 will be cached 0 * 0 = 0
1 will be cached 1 * 1 = 1
2 will be cached 2 * 2 = 4
3 will be cached 3 * 3 = 9
0 taken from cache 0 * 0 = 0
1 taken from cache 1 * 1 = 1
2 taken from cache 2 * 2 = 4
3 taken from cache 3 * 3 = 9
*/
//////////////second example///////////////////
Copyright copyright = new Copyright();
CacheComputeManager manager2 = new CacheComputeManager(copyright);
System.out.println(manager2.compute(new Copyright.Period(3012, 3147)));
System.out.println(manager2.compute(new Copyright.Period(3012, 3146)));
System.out.println(manager2.compute(new Copyright.Period(3012, 3147)));
/* output
Period{firstYear=3012, secondYear=3147} will be cached All rights reserved (c) 3012-3147
Period{firstYear=3012, secondYear=3146} will be cached All rights reserved (c) 3012-3146
Period{firstYear=3012, secondYear=3147} taken from cache All rights reserved (c) 3012-3147
*/
}
}
package com.javarush.test.level29.lesson09.bonus01;
public class Square implements Computable<Integer, Integer>{
@Override
public Integer compute(Integer integer) throws InterruptedException {
int val = integer.intValue();
return val * val;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment