定义任务的内容
多线程编程的核心元素就是任务,任务是独立的活动.不依赖其他任务的状态,结果,以及边界效应.
定义任务的内容使用Runnable和Callable.
多线程编程的核心元素就是任务,任务是独立的活动.不依赖其他任务的状态,结果,以及边界效应.
定义任务的内容使用Runnable和Callable.
如果是synchronized(this),那么和synchronized 方法没有任何区别,锁定对象都是方法所在的对象.
synchronized void mymethod() { ... }
void mymethod() {
synchronized (this) { ... }
}
public final long incrementAndGet() {
for (;;) {
long current = get();
long next = current + 1;
if (compareAndSet(current, next))
return next;
}
}
//in java 8:
public final long incrementAndGet() {
return unsafe.getAndAddLong(this, valueOffset, 1L) + 1L;
}
CountDownLatch和CyclicBarrier示例
samples code and notes on java == and equals