class(引用类型)的多个实例是否使其成为线程安全的?
Does having multiple instances of a class (Reference Type) make it thread safe?
希望你能帮助我澄清我对线程安全和引用类型的多个实例的理解。
我在下面概述了一个简单的场景。我的问题是:如果 ProcessorOne 和 ProcessorTwo 同时 运行,并且 DatabaseIntegration class 是引用类型并且 ProcessorOne 和 ProcessorTwo 都有自己的 DataBaseIntegration 实例,那么ProcessorOne 保存的实体阻止了 ProcessorTwo 的名称?
public class ProcessorOne
{
private readonly DatabaseIntegration databaseIntegration;
public ProcessorOne(DatabaseIntegration databaseIntegration)
{
this.databaseIntegration = databaseIntegration;
}
public void Process()
{
this.databaseIntegration.SetName("NameOne")
Entity entity = new Entity()
this.dataBaseIntegration.Save(entity);
}
}
public class ProcessorTwo
{
private readonly DatabaseIntegration dataBaseIntegration;
public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
{
this.dataBaseIntegration = dataBaseIntegration;
}
public void Process()
{
this.databaseIntegration.SetName("NameTwo")
Entity entity = new Entity()
this.dataBaseIntegration.Save(entity);
}
}
public class DatabaseIntegration
{
private string entityName;
public void SetName(string entityName)
{
this.entityName = entityName;
}
public void Save(Entity entity)
{
entity.EntityName = this.entityName;
using (DbContext context = new DbContext(sqlConnection))
{
context.Entity.Add(entity);
context.SaveChanges();
}
}
}
不,它们会有不同的名称,因为正如您所说,在您的代码中每个 class 都有不同的 DatabaseIntegration 实例。
因此 ProcessorOne 的 dataBaseIntegration 名称为 "NameOne",而 ProcessorTwo 的 dataBaseIntegration 名称为 "NameTwo"。
此致。
答案是肯定的,它们是线程安全的。但是为什么不在创建它们时设置实体名称。在这种情况下,即使您使用相同的 DatabaseIntegration 对象,它仍然是线程安全的,因为每次调用 Save() 时都会传递不同的实体对象。
public class ProcessorOne
{
private readonly DatabaseIntegration databaseIntegration;
public ProcessorOne(DatabaseIntegration databaseIntegration)
{
this.databaseIntegration = databaseIntegration;
}
public void Process()
{
Entity entity = new Entity(){entityName = "NameOne"}
this.dataBaseIntegration.Save(entity);
}
}
public class ProcessorTwo
{
private readonly DatabaseIntegration dataBaseIntegration;
public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
{
this.dataBaseIntegration = dataBaseIntegration;
}
public void Process()
{
Entity entity = new Entity(){entityName = "NameTwo"}
this.dataBaseIntegration.Save(entity);
}
}
public class DatabaseIntegration
{
public void Save(Entity entity)
{
using (DbContext context = new DbContext(sqlConnection))
{
context.Entity.Add(entity);
context.SaveChanges();
}
}
}
希望你能帮助我澄清我对线程安全和引用类型的多个实例的理解。
我在下面概述了一个简单的场景。我的问题是:如果 ProcessorOne 和 ProcessorTwo 同时 运行,并且 DatabaseIntegration class 是引用类型并且 ProcessorOne 和 ProcessorTwo 都有自己的 DataBaseIntegration 实例,那么ProcessorOne 保存的实体阻止了 ProcessorTwo 的名称?
public class ProcessorOne
{
private readonly DatabaseIntegration databaseIntegration;
public ProcessorOne(DatabaseIntegration databaseIntegration)
{
this.databaseIntegration = databaseIntegration;
}
public void Process()
{
this.databaseIntegration.SetName("NameOne")
Entity entity = new Entity()
this.dataBaseIntegration.Save(entity);
}
}
public class ProcessorTwo
{
private readonly DatabaseIntegration dataBaseIntegration;
public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
{
this.dataBaseIntegration = dataBaseIntegration;
}
public void Process()
{
this.databaseIntegration.SetName("NameTwo")
Entity entity = new Entity()
this.dataBaseIntegration.Save(entity);
}
}
public class DatabaseIntegration
{
private string entityName;
public void SetName(string entityName)
{
this.entityName = entityName;
}
public void Save(Entity entity)
{
entity.EntityName = this.entityName;
using (DbContext context = new DbContext(sqlConnection))
{
context.Entity.Add(entity);
context.SaveChanges();
}
}
}
不,它们会有不同的名称,因为正如您所说,在您的代码中每个 class 都有不同的 DatabaseIntegration 实例。
因此 ProcessorOne 的 dataBaseIntegration 名称为 "NameOne",而 ProcessorTwo 的 dataBaseIntegration 名称为 "NameTwo"。
此致。
答案是肯定的,它们是线程安全的。但是为什么不在创建它们时设置实体名称。在这种情况下,即使您使用相同的 DatabaseIntegration 对象,它仍然是线程安全的,因为每次调用 Save() 时都会传递不同的实体对象。
public class ProcessorOne
{
private readonly DatabaseIntegration databaseIntegration;
public ProcessorOne(DatabaseIntegration databaseIntegration)
{
this.databaseIntegration = databaseIntegration;
}
public void Process()
{
Entity entity = new Entity(){entityName = "NameOne"}
this.dataBaseIntegration.Save(entity);
}
}
public class ProcessorTwo
{
private readonly DatabaseIntegration dataBaseIntegration;
public ProcessorTwo(DatabaseIntegration dataBaseIntegration)
{
this.dataBaseIntegration = dataBaseIntegration;
}
public void Process()
{
Entity entity = new Entity(){entityName = "NameTwo"}
this.dataBaseIntegration.Save(entity);
}
}
public class DatabaseIntegration
{
public void Save(Entity entity)
{
using (DbContext context = new DbContext(sqlConnection))
{
context.Entity.Add(entity);
context.SaveChanges();
}
}
}