Guava 병발 (1) - Monitor 의 사용

6445 단어 guavaJAVA 및 발주
설명
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

총화
 생략

좋은 웹페이지 즐겨찾기