Guava 병발 (1) - Monitor 의 사용
Monitor 는 자바 본토의 synchronized, ReentrantLock 처럼 매번 하나의 스 레 드 만 실행 하고 다시 사용 할 수 있 으 며, 매번 점용 할 때마다 종료 점용 에 대응 합 니 다.
Monitor.enter // Monitor , Monitor.leave
Monitor.enterWhen // Monitor , Monitor.leave
Monitor.tryEnter // Monitor ,true , false ,
Monitor.tryEnterIf // Monitor
Monitor. Guard 는 스 레 드 가 막 히 는 조건 입 니 다.
Monitor 협조 사용.
Monitor.Guard listBelowCapacity = new
Monitor.Guard(monitor) {
@Override
public boolean isSatisfied() {
。。。。。
}
};
코드
package com.wll.guava.concurrent;
import com.google.common.util.concurrent.Monitor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Monitor synchronized ReentrantLocks ,
*/
public class MonitorExample {
private static final int MAX_SIZE = 10;
private Monitor monitor = new Monitor();
private List list = new ArrayList();
Monitor.Guard listBelowCapacity = new
Monitor.Guard(monitor) {
@Override
public boolean isSatisfied() {
return list.size() < MAX_SIZE;
}
};
public void addToListWait(String item) throws InterruptedException {
// MAX_SIZE, ( )
this.monitor.enterWhen(listBelowCapacity);
try {
list.add(item);
System.out.println(" [" + item + "] , List.size=" + list.size() + "~");
} finally { // Monitor
monitor.leave();
}
}
public void addToListSkipWait(String item) throws InterruptedException {
// MAX_SIZE,
//this.monitor.enterWhen(listBelowCapacity);
// false
Boolean isOK = monitor.tryEnterIf(listBelowCapacity);
System.out.println("Thread[" + Thread.currentThread().getName() + "] item=" + item + ", :isOK=" + isOK);
if (isOK) {
try {
list.add(item);
System.out.println(" [" + item + "] , List.size=" + list.size() + "~");
} finally { // Monitor
monitor.leave();
}
}
}
public static void main(String[] args) {
final MonitorExample monitorDemo = new MonitorExample();
for (int i = 0; i < 5; i++) {
new Thread() {
public void run() {
for (int count = 0; count < 6; count++) {
try {
// monitorDemo.addToListWait(count + "------------------>" + Thread.currentThread().getName());
monitorDemo.addToListSkipWait(count + "------------------>" + Thread.currentThread().getName());
Thread.sleep(100L);
} catch (Exception e) {
System.out.println(e);
}
}
}
}.start();
}
//
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("---------------------------- ---------------------------");
Iterator iteratorStringList = monitorDemo.list.iterator();
while (iteratorStringList.hasNext()) {
System.out.println(iteratorStringList.next());
}
}
}
3. 집행 결과
Connected to the target VM, address: '127.0.0.1:61207', transport: 'socket'
Thread[Thread-2] item=0------------------>Thread-2, :isOK=false
Thread[Thread-1] item=0------------------>Thread-1, :isOK=false
Thread[Thread-4] item=0------------------>Thread-4, :isOK=false
Thread[Thread-0] item=0------------------>Thread-0, :isOK=false
Thread[Thread-3] item=0------------------>Thread-3, :isOK=true
[0------------------>Thread-3] , List.size=1~
Thread[Thread-3] item=1------------------>Thread-3, :isOK=true
[1------------------>Thread-3] , List.size=2~
Thread[Thread-2] item=1------------------>Thread-2, :isOK=true
Thread[Thread-0] item=1------------------>Thread-0, :isOK=false
Thread[Thread-4] item=1------------------>Thread-4, :isOK=false
[1------------------>Thread-2] , List.size=3~
Thread[Thread-1] item=1------------------>Thread-1, :isOK=false
Thread[Thread-2] item=2------------------>Thread-2, :isOK=true
[2------------------>Thread-2] , List.size=4~
Thread[Thread-0] item=2------------------>Thread-0, :isOK=true
[2------------------>Thread-0] , List.size=5~
Thread[Thread-4] item=2------------------>Thread-4, :isOK=true
[2------------------>Thread-4] , List.size=6~
Thread[Thread-1] item=2------------------>Thread-1, :isOK=true
[2------------------>Thread-1] , List.size=7~
Thread[Thread-3] item=2------------------>Thread-3, :isOK=true
[2------------------>Thread-3] , List.size=8~
Thread[Thread-3] item=3------------------>Thread-3, :isOK=true
[3------------------>Thread-3] , List.size=9~
Thread[Thread-2] item=3------------------>Thread-2, :isOK=false
Thread[Thread-0] item=3------------------>Thread-0, :isOK=true
[3------------------>Thread-0] , List.size=10~
Thread[Thread-1] item=3------------------>Thread-1, :isOK=false
Thread[Thread-4] item=3------------------>Thread-4, :isOK=false
Thread[Thread-2] item=4------------------>Thread-2, :isOK=false
Thread[Thread-1] item=4------------------>Thread-1, :isOK=false
Thread[Thread-4] item=4------------------>Thread-4, :isOK=false
Thread[Thread-3] item=4------------------>Thread-3, :isOK=false
Thread[Thread-0] item=4------------------>Thread-0, :isOK=false
Thread[Thread-3] item=5------------------>Thread-3, :isOK=false
Thread[Thread-2] item=5------------------>Thread-2, :isOK=false
Thread[Thread-4] item=5------------------>Thread-4, :isOK=false
Thread[Thread-0] item=5------------------>Thread-0, :isOK=false
Thread[Thread-1] item=5------------------>Thread-1, :isOK=false
---------------------------- ---------------------------
0------------------>Thread-3
1------------------>Thread-3
1------------------>Thread-2
2------------------>Thread-2
2------------------>Thread-0
2------------------>Thread-4
2------------------>Thread-1
2------------------>Thread-3
3------------------>Thread-3
3------------------>Thread-0
Disconnected from the target VM, address: '127.0.0.1:61207', transport: 'socket'
Process finished with exit code 0
총화
생략
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Guava 학습노트-Function, Predicate먼저 실례에 필요한 실체 클래스를 도입하고 실체 클래스의 get/set 방법은 생략합니다 하나의 Function 인터페이스 구현: Function TestCase: Predicate Predicate 인터페이스 구현...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.