0

こんにちは、以下のコードがあり、行でエラーが発生します:

uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer));

LikeTimestampSynchronizerは変数として解決できませんでした。

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Random;
import java.util.UUID;
import javax.crypto.KeyGenerator;
import com.fasterxml.uuid.EthernetAddress;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.TimestampSynchronizer;
import com.fasterxml.uuid.UUIDTimer;
import com.fasterxml.uuid.ext.FileBasedTimestampSynchronizer;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.google.common.base.Charsets;
import com.google.common.io.BaseEncoding;
import com.google.gdata.util.common.util.*;

public class UUID_Test {

 public static void main(String[] args) {
  for (int i = 0; i < 10000; i++) {
   try {
    UUID_Test.uuidToBase32();
} catch (IOException e) {
    
    e.printStackTrace();
}
  }
 }

 private static String uuidToBase32() throws IOException 
 {
       
        EthernetAddress nic = EthernetAddress.fromInterface();
        TimeBasedGenerator uuidGenerator;   
            uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer)); 
        ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
        bb.putLong(uuidGenerator.generate().getMostSignificantBits());
        bb.putLong(uuidGenerator.generate().getLeastSignificantBits());
        
        return BaseEncoding.base32().encode(bb.array());
    
 
}
}

これを解決するには?
そして、私が使用するとき

   uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), new FileBasedTimestampSynchronizer()));

    Getting Exception
Exception in thread "main" java.nio.channels.OverlappingFileLockException

または私が書いた場合

 uuidGenerator = Generators.timeBasedGenerator(nic,new FileBasedTimestampSynchronizer(new File("d://abc"), new File("d://def")));

Getting Exception

WARNING: (file 'd:\abc') Missing or empty file, can not read timestamp value
WARNING: (file 'd:\def') Missing or empty file, can not read timestamp value
WARNING: Could not determine safe timer starting point: assuming current system time is acceptable
GNKOP7JVWII6HMAJ2S7NSZXSYE
Exception in thread "main" java.nio.channels.OverlappingFileLockException
    at sun.nio.ch.SharedFileLockTable.checkList(FileLockTable.java:255)

ありがとう

編集

コメントから回答にコピーされたコード:

コードを静的ブロックに配置します

static EthernetAddress nic = EthernetAddress.fromInterface();
static File f = new File("D://a.txt");
static File f1 = new File("D://f.txt");
static TimeBasedGenerator uuidGenerator;

static {
    try {
         uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), new FileBasedTimestampSynchronizer(f, f1)));
    }
    catch (IOException e) {
         e.printStackTrace();
    }
 }

その与える例外

java.io.IOException: Failed to lock 'd://a.txt' (another JVM running UUIDGenerator?)
4

1 に答える 1

0

TimestampSynchronizer は非パブリック クラスであるため、コードで使用することはできません。

重複するロックの問題は、この行から発生します

uuidGenerator = Generators.timeBasedGenerator(nic, new UUIDTimer(new Random(), TimestampSynchronizer)); 

毎回新しいジェネレーターを作成しています - 時間ベースの UUID の一意性を確保するために、これはロック ファイルを使用します。ロック ファイルは、1 つのジェネレーターに対してのみ使用できます。

解決策: タイマーを 1 つだけ作成するか、ジェネレーターを 1 つだけ作成して、ループ内で再利用します。

于 2013-10-15T16:33:41.697 に答える