复制代码代码如下 :
패키지 com.ysh.file;
java.util.arraylist 가져 오기;
java.util.linkedList 가져 오기;
Java.util.list 가져 오기;
java.util.concurrent.locks.condition import;
java.util.concurrent.locks.lock import;
java.util.concurrent.locks.reentrantlock import;
import com.google.common.collect.lists;
공개 클래스 쓰기 queue {
개인 정적 최종 최종 int max_queue_size = 10000;
Private LinkedList <string> queuecache = lists.newLinkedList ();
개인 잠금 잠금 = 새로운 재 렌트 링크 ();
개인 조건 full = lock.newcondition ();
비공개 조건 빈 = lock.newcondition ();
Private WriteQueue () {
}
개인 정적 클래스 쓰기 queueUlder {
개인 최종 정적 쓰기 큐 인스턴스 = 새로운 writequeue ();
}
public static writequeue getInstance () {
Return WriteQueueHolder.instance;
}
public void add (문자열 줄) {
lock.lock ();
노력하다 {
while (queuecache.size ()> = max_queue_size) {
full.await ();
}
queuecache.addfirst (line);
empty.signal ();
} catch (InterruptedException e) {
e.printstacktrace ();
} 마지막으로 {
lock.unlock ();
}
}
public void add (list <string> linelist) {
lock.lock ();
노력하다 {
while (queuecache.size ()> = max_queue_size) {
full.await ();
}
queuecache.addall (linelist);
empty.signal ();
} catch (InterruptedException e) {
e.printstacktrace ();
} 마지막으로 {
lock.unlock ();
}
}
public arraylist <string> flush () {
ArrayList <string> temp = null;
lock.lock ();
노력하다 {
while (queuecache.size () == 0) {
empty.await ();
}
temp = lists.newarrayList ();
temp.addall (queuecache);
queuecache.clear ();
full.signal ();
} catch (InterruptedException e) {
e.printstacktrace ();
} 마지막으로 {
lock.unlock ();
}
반환 온도;
}
}