# java8 concurrent
1. [ - ThreadsExecutors](https://zhuanlan.zhihu.com/p/33266682)
2. [ - ](http://zhuanlan.zhihu.com/p/33267015)
3. [ - ConcurrentMap](https://zhuanlan.zhihu.com/p/33267165)
## Thread Runnable
Java
CPUcore
JavaJDK1.0taskRunnable `run()`
##
`new Thread()`
1.
2.
3. oom
4.
###
**newCachedThreadPool**
60s `Integer.MAX_VALUE`
```java
public void cachedThreadPoolDemo(){
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 5; i++) {
final int index = i;
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```
```bash
pool-1-thread-1, index=0
pool-1-thread-1, index=1
pool-1-thread-1, index=2
pool-1-thread-1, index=3
pool-1-thread-1, index=4
```
pool-1-thread-1
**newFixedThreadPool**
`LinkedBlockingQueue`
```java
public void fixedThreadPoolDemo(){
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
for (int i = 0; i < 6; i++) {
final int index = i;
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```
```bash
pool-1-thread-1, index=0
pool-1-thread-2, index=1
pool-1-thread-3, index=2
pool-1-thread-1, index=3
pool-1-thread-2, index=4
pool-1-thread-3, index=5
```
31s `Runtime.getRuntime().availableProcessors()`
**newSingleThreadExecutor**
LinkedBlockingQueue(FIFO)
```java
public void singleThreadExecutorDemo(){
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 3; i++) {
final int index = i;
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", index="+index);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
```
```bash
pool-1-thread-1, index=0
pool-1-thread-1, index=1
pool-1-thread-1, index=2
```
**newScheduledThreadPool**
```java
public void scheduledThreadPoolDemo(){
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(3);
//1s
scheduledThreadPool.schedule(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", delay 1s");
}
}, 1, TimeUnit.SECONDS);
//2s3s
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+", every 3s");
}
}, 2, 3, TimeUnit.SECONDS);
}
```
```bash
pool-1-thread-1, delay 1s
pool-1-thread-1, every 3s
pool-1-thread-2, every 3s
pool-1-thread-2, every 3s
...
```
- `schedule(Runnable command, long delay, TimeUnit unit)`: `Runnable`
- `schedule(Callable callable, long delay, TimeUnit unit)`: `Callable`
- `scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)`: period
- `scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,TimeUnit unit)`: `scheduleAtFixedRate()`
scheduleWithFixedDelay()
scheduleAtFixedRate()
> ScheduledExecutorService
****
| | corePoolSize | maximumPoolSize | keepAliveTime | workQueue |
|:-----:|:--------|:-------|:-------|:-------|
| `newCachedThreadPool` | 0 | Integer.MAX_VALUE | 60s `SynchronousQueue` |
| `newFixedThreadPool` | nThreads | nThreads | 0 | `LinkedBlockingQueue` |
| `newSingleThreadExecutor` | 1 | 1 | 0 | `LinkedBlockingQueue` |
| `newScheduledThreadPool` | corePoolSize | Integer.MAX_VALUE | 0 | `DelayedWorkQueue` |
`DefaultThreadFactory` `ThreadPoolExecutor.AbortPolicy`
## Lock
Java 5 java.util.concurrent.locks Lock `synchronized`
Lock 3 ReentrantLockReetrantReadWriteLock.ReadLock ReetrantReadWriteLock.WriteLock
lock ReentrantLock try finally return return try unlock() lock
```java
//true
Lock lock = new ReentrantLock();
lock.lock();
try {
//
//
// return
} finally {
//finally
lock.unlock();
}
```
JAVA `ReentrantLock` `synchronized`
**ReentrantReadWriteLock**
jvm
`ReentrantReadWriteLock`
-
-
-
-
-
-
## StampedLock
`StampedLock` java 8 `java.util.concurrent.locks` API
`ReentrantReadWriteLock`
`ReentrantReadWriteLock`
`StampedLock` /StampedLock
stamp0
stamps
- writeLockstampunlockWritetryWriteLock
- readLockstampunlockReadtryReadLock
- `tryOptimisticRead` 0 stampvalidatetrue
> Pessimistic Lockblock
>
> Java synchronized block
> Optimistic Lock
>
## AtomicInteger
JDK1.5java.util.concurrent.atomic
AtomicBooleanAtomicIntegerAtomicLongAtomicReference
,.
AtomicIntegerInteger
Java++ii++synchronized
AtomicInteger
```java
public final int get() //
public final int getAndSet(int newValue)//
public final int getAndIncrement() //
public final int getAndDecrement() //
public final int getAndAdd(int delta) //
```
## LongAccumulator
`LongAdder` jdk1.8 `Striped64`
AtomicLongLongAdderAtomicLong
`LongAccumulator` `LongAdder` Striped64LongAdder()
LongAdderLongAccumulator
## Semaphore
Semaphore
SemaphoreXX
XX
XX5
****
Semaphore
IO
10
Semaphore
##
- [Java](http://gityuan.com/2016/01/16/thread-pool/)
- [Java](http://zhwbqd.github.io/2015/02/13/lock-in-java.html)
- [JavaAtomicInteger](https://www.jianshu.com/p/509aca840f6d)
- [Jdk1.8 JUC LongAdderLongAccumulator](http://brokendreams.iteye.com/blog/2259858)
- [StampedLock](http://www.importnew.com/14941.html)
- [Java8 StampedLock](https://coderbee.net/index.php/concurrent/20140628/947)
- [Semaphore](http://ifeve.com/tag/semaphore/)