接上一篇《java并发编程(三)客户端加锁与组合》
最近事太多,耽误了两天时间没有写博客,感觉每天不记录下自己的学习东西就想没学一样,这两天的事实在太多,不管是学业上的 还是感情上的,闹心,程序员还是把踏入爱河的事放一放吧~对了 最近接了一本《HTTP权威指南》,想深入学习下,赶紧把Java 并发访问的相关知识 总结完成就开始,另外,在某米音乐的朋友 说 java web 已经很难找工作了,现在是php的天下了,心里一颤,然后继续学习java,你懂的,好了不扯了,继续今天的内容。
java提供了很多的同步工具类,简化了编程过程,而且减少了bug,当然我们完全可以自己写同步工具类,但是 我们不是说好的 要站在巨人的肩膀上 编程的吗。
public long timeTasks(int nThread, final Runnable task)
throws InterruptedException {
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(nThread);
for (int i = 0; i < nThread; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
startGate.await();
try {
task.run();
} finally {
endGate.countDown();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
task.run();
}
}).start();
}
long start = System.nanoTime();
startGate.countDown();
endGate.await();
long end = System.nanoTime();
return end - start;
}
public class PreLoader {
private final FutureTask future = new FutureTask(
new Callable() {
@Override
public ProductInfo call() throws Exception {
return loaderProductInfo();
}
});
private final Thread thread = new Thread(future);
public void start() {
thread.start();
}
public ProductInfo get() throws DataLoadException, InterruptedException {
try {
return future.get();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof DataLoadException)
throw (DataLoadException) cause;
else
throw launderThrowable(cause);
}
}
private static RuntimeException launderThrowable(Throwable t) {
if (t instanceof RuntimeException)
return (RuntimeException) t;
else if (t instanceof Error)
throw (Error) t;
else
throw new IllegalStateException("Not unchecked ", t);
}
ProductInfo loaderProductInfo() {
// 耗时事件
return null;
}
}
public class BoundedHash {
private final Set set;
private final Semaphore sem;
public BoundedHash(int permits) {
set = Collections.synchronizedSet(new HashSet());
sem = new Semaphore(permits);
}
public boolean add(T t) throws InterruptedException {
sem.acquire();
boolean wasAdded = false;
try {
wasAdded = set.add(t);
return wasAdded;
} finally {
if (!wasAdded)
sem.release();
}
}
public boolean remove(T t) {
boolean wasRemoved = set.remove(t);
if (wasRemoved)
sem.release();
return wasRemoved;
}
}
public class CellularAutomata {
private final Board mainBoard;
private final CyclicBarrier barrier;
private final Worker[] workers;
public CellularAutomata(Board board) {
this.mainBoard = board;
int count = Runtime.getRuntime().availableProcessors();
this.barrier = new CyclicBarrier(count, new Runnable() {
@Override
public void run() {
}
});
this.workers = new Worker[count];
for (int i = 0; i < count; i++) {
workers[i] = new Worker(mainBoard.getSubBoard(count, i));
}
}
private class Worker implements Runnable {
private final Board board;
public Worker(Board board) {
this.board = board;
}
@Override
public void run() {
while (!board.hasConvergeted()) {
for (int i = 0; i < board.getMaxx(); i++)
for (int j = 0; j < board.getMaxy(); j++)
board.setNewValue(i, j, computeValue(i, j));
try {
barrier.await(); //等待 直到达到线程数
} catch (InterruptedException | BrokenBarrierException e) {
return;
}
}
}
}
public void start() {
for (int i = 0; i < workers.length; i++) {
new Thread(workers[i]).start();
mainBoard.waitForConvergence();
}
}
}
interface Computable {
V compute(K arg) throws InterruptedException;
}
public class Memoizer implements Computable {
private final Map> cache = new ConcurrentHashMap>();
private final Computable c;
public Memoizer(Computable c) {
this.c = c;
}
@Override
public V compute(K arg) throws InterruptedException {
while (true) {
Future f = cache.get(arg);
if (f == null) {
Callable eval = new Callable() {
@Override
public V call() throws Exception {
return c.compute(arg);
}
};
FutureTask ft = new FutureTask(eval);
f = cache.put(arg, ft);
if (f == null) {
f = ft;
ft.run();
}
}
try {
return f.get();
} catch (CancellationException e) {
cache.remove(arg, f);
} catch (ExecutionException e) {
throw new InterruptedException();
}
}
}
}
It’s the mutable state, stupid.1
All concurrency issues boil down to coordinating access to mutable
state. The less mutable state, the easier it is to ensure thread safety.
Make fields final unless they need to be mutable.
Immutable objects are automatically thread-safe.
Immutable objects simplify concurrent programming tremendously.They are simpler and safer, and can be shared freely without lockingor defensive copying.
Encapsulation makes it practical to manage the complexity.
You could write a thread-safe program with all data stored in globalvariables, but why would you want to? Encapsulating data withinobjects makes it easier to preserve their invariants; encapsulatingsynchronization within objects makes it easier to comply with theirsynchronization policy.
Guard each mutable variable with a lock.
Guard all variables in an invariant with the same lock.
Hold locks for the duration of compound actions.
A program that accesses a mutable variable from multiple threadswithout synchronization is a broken program.
Don’t rely on clever reasoning about why you don’t need to synchro-nize.
Include thread safety in the design process—or explicitly documentthat your class is not thread-safe.
Document your synchronization policy.