在我的编码中,我想获得一些性能改进,所以我认为线程是一个选择,但我对多线程和数据共享问题没有信心。有人可以查看这些代码并给我任何建议,谢谢!
public partial class WorkflowService: IWorkflowService
{
public void StartProcess(WfAppRunner starter)
{
try
{
WfRuntimeManager runtimeManager = new WfRuntimeManagerStartup();
runtimeManager.AppRunner = starter;
runtimeManager.Execute();
}
catch (System.Exception)
{
throw;
}
}
}
internal abstract class WfRuntimeManager
{
internal WfAppRunner AppRunner { get; set; }
internal abstract void ExecuteInstance();
internal bool Execute()
{
try
{
Thread thread = new Thread(ExecuteInstance);
thread.Start();
return true;
}
catch (System.Exception)
{
throw;
}
}
}
internal class WfRuntimeManagerStartup : WfRuntimeManager
{
internal override void ExecuteInstance()
{
ISession session = SessionFactory.CreateSession();
try
{
var runner = base.AppRunner;
//do some works with runner, and save works to database.
SaveToDatabase(runner);
...
//do some other works
ProcessOtherWorks();
session.Commit();
}
catch (System.Exception)
{
session.Rollback();
}
finally
{
session.Dispose();
}
}
}
//do some test, minic the true user case
[TestMethod]
public void RunParalleled()
{
//StarterA:
//{"UserID":"10","UserName":"Long","AppName":"SamplePrice","AppInstanceID":"100","ProcessGUID":"072af8c3-482a-4b1c-890b-685ce2fcc75d"}
var starterA = new WfAppRunner();
starterA.ProcessGUID = Guid.Parse("072af8c3-482a-4b1c-890b-685ce2fcc75d");
starterA.UserID = 10;
starterA.UserName = "Long";
//SarterB:
//{"AppName":"Offin","AppInstanceID":587,"UserID":"0021","UserName":"test2","ProcessGUID":"68696ea3-00ab-4b40-8fcf-9859dbbde378","FlowStatus":null,"Conditions":{"surplus":"aa"}}
var starterB = new WfAppRunner();
starterB.ProcessGUID = Guid.Parse("68696ea3-00ab-4b40-8fcf-9859dbbde378");
starterB.UserID = 21;
starterB.UserName = "test2";
IWorkflowService serviceA, serviceB;
for (var i = 0; i < 5000; i++)
{
serviceA = new WorkflowService();
starterA.AppName = "Price";
starterA.AppInstanceID = i;
serviceB = new WorkflowService();
starterB.AppName = "Offin";
starterB.AppInstanceID = i;
//execute process instance
serviceA.StartProcess(starterA);
serviceB.StartProcess(starterB);
}
}