私は良い方法で願っています:-)私はこのコードを書きました。私がやりたかったのは、「キャッシュ」のようなものを構築することです。多くの呼び出しがそのクラスに到達する可能性があるため、さまざまなスレッドを監視する必要があると想定したため、ThreadLocal 機能を試しました。基本パターンには「VECTOR の多数のセット」があります。ベクターには次のようなものがあります。 VECTOR.FieldName = "X" VECTOR.FieldValue= "Y" セット内の非常に多くの Vector オブジェクト。異なるマシン、ユーザー、オブジェクトからの異なる呼び出しに対して異なるセット。
private static CacheVector instance = null;
private static SortedSet<SplittingVector> s = null;
private static TreeSet<SplittingVector> t = null;
private static ThreadLocal<SortedSet<SplittingVector>> setOfVectors = new ThreadLocal<SortedSet<SplittingVector>>();
private static class MyComparator implements Comparator<SplittingVector> {
public int compare(SplittingVector a, SplittingVector b) {
return 1;
}
// No need to override equals.
}
private CacheVector() {
}
public static SortedSet<SplittingVector> getInstance(SplittingVector vector) {
if (instance == null) {
instance = new CacheVector();
//TreeSet<SplittingVector>
t = new TreeSet<SplittingVector>(new MyComparator());
t.add(vector);
s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
CacheVector.assign(s);
} else {
//TreeSet<SplittingVector> t = new TreeSet<SplittingVector>();
t.add(vector);
s = Collections.synchronizedSortedSet(t);//Sort the set of vectors
CacheVector.assign(s);
}
return CacheVector.setOfVectors.get();
}
public SortedSet<SplittingVector> retrieve() throws Exception {
SortedSet<SplittingVector> set = setOfVectors.get();
if (set == null) {
throw new Exception("SET IS EMPTY");
}
return set;
}
private static void assign(SortedSet<SplittingVector> nSet) {
CacheVector.setOfVectors.set(nSet);
}
だから...私はそれを添付して、次のように使用します:
CachedVector cache = CachedVector.getInstance(bufferedline);
良い点: Bufferedline は、データ ファイルの区切り文字に基づいて分割された行です。ファイルは任意のサイズにすることができます。
では、このコードをどのように見ますか? 私は心配する必要がありますか?このメッセージのサイズについてお詫び申し上げます。