diff --git a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs index d53a87d23..6ee473ccf 100644 --- a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs +++ b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/IdWorker.cs @@ -65,7 +65,10 @@ namespace SqlSugar.DistributedSystem.Snowflake // def get_timestamp() = System.currentTimeMillis readonly object _lock = new Object(); - + public long getID() + { + return NextId(); + } public virtual long NextId() { lock(_lock) diff --git a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs index 1a4bb6cef..c98848d4a 100644 --- a/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs +++ b/Src/Asp.Net/SqlSugar/DistributedSystem/Snowflake/SnowFlakeSingle.cs @@ -8,22 +8,38 @@ namespace SqlSugar { public sealed class SnowFlakeSingle { - public static readonly SnowFlakeSingle instance = new SnowFlakeSingle(); + private static object LockObject = new object(); + private static DistributedSystem.Snowflake.IdWorker worker; public static int WorkId = 1; public static int DatacenterId = 1; private SnowFlakeSingle() { - worker = new DistributedSystem.Snowflake.IdWorker(WorkId, DatacenterId); + } static SnowFlakeSingle() { } - public static SnowFlakeSingle Instance + public static DistributedSystem.Snowflake.IdWorker Instance { - get { return instance; } + get + { + if (worker == null) + { + lock (LockObject) + { + if (worker == null) + { + worker = new DistributedSystem.Snowflake.IdWorker(WorkId, DatacenterId); + } + } + } + return worker; + } } - private DistributedSystem.Snowflake.IdWorker worker; - public long getID() - { - return worker.NextId(); + public static DistributedSystem.Snowflake.IdWorker instance + { + get + { + return Instance; + } } } }