类中具有状态和可变对象的多线程计算 [英] Multithreading calculations with state and mutable objects in a class

查看:70
本文介绍了类中具有状态和可变对象的多线程计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习多线程,但是我不知道如何在学习场景中实现线程安全.

I'm learning about multi threading, however I can't figure out how can I achieve thread-safe in my study scenario.

这是一个伪代码,所以我有一个类,可以进行一些计算并共享一些公共数据(公式"列表),并具有一个公共属性ID,以供进一步阅读:

This is a pseudo code, so I have a class that do some calculations and share some public data (the Formulas list), and have a public property Id to read further:

class Problem{
  public int Id {get; set;}
  public IList<Calc> Formulas {get; private set;}

  public Problem(int id){
    Id = id;
    Formulas = new List<Formulas>();
  }

  public void SolveProblem(){
    Calc newCalc = DoSomeCalculations();
    Formulas.Add(newCalc);
  }

  private Calc DoSomeCalculations(){
    var originalFormulas = Formulas.Where(x => x.Number > 2);
    return new Calc(originalFormulas);
  }
}

我做了一个类,可以通过许多线程运行很多计算:

And I did a class to runs a lot of calculations by many threads:

class ProblemsRunner{
  public void Run(List<Problem> problemsToSolve){
    var thread1 = new Thread(RunProblem(problemsToSolve.Take(10)));
    var thread2 = new Thread(RunProblem(problemsToSolve.Skip(10).Take(10)));
    var thread3 = new Thread(RunProblem(problemsToSolve.Skip(20).Take(10)));

    thread1.Start();
    thread2.Start();
    thread3.Start();
  }

  private void RunProblem(IEnumerable<Problem> problems){
    foreach(var problem in problems){
      problem.SolveProblem();
    }
  }
}

我不知道我的代码中有什么问题:

I don't to know what is my concern in my code:

  • 是问题类共享的数据(公式列表和属性ID),我只需要锁定其访问权限?

  • Is the data that class Problem shares (Formulas list, and property Id), and I just need to lock its access?

我是否需要担心ProblemsRunner,并在foreach循环中将整个对象锁定在ProblemsRunner.RunProblem()中?

Do I need to worry in ProblemsRunner, and lock the entire object inside ProblemsRunner.RunProblem() foreach loop?

由于计算之后,我需要知道每个对象的Id和公式,并且我无法弄清楚如何使用多线程.

Because after calculations, I need to know both Id and Formulas of each object, and I can't figure out how I do this with multithreading.

您不需要更正所有代码(但请放心),我正在寻求一些指导来解决此问题,并实现线程安全性……在类正确性内使用状态和可变对象 >

You don't need to correct all the code (but feel free), I'm asking for some directions to solve this problem, and achieve thread-safety...with states and mutable objects inside a class correclty

推荐答案

您的代码似乎没有您担心的任何问题.每个问题都有自己的Formulas,因此在解决问题后添加新公式时,您不会访问任何可能从另一个线程访问的内容.

Your code doesn't seem to have any of the problems you're worried about. Each problem has its own Formulas, so when you add a new formula after solving the problem, you're not accessing anything that might get accessed from another thread.

我怀疑那不是您想要的.您可能只需要一个Formulas;在那种情况下,您肯定会想要一个线程安全的列表.

I suspect that that isn't what you intended, though. You probably wanted a single Formulas; in that case you certainly would want a thread-safe list.

您应该使用某种线程安全的阻塞队列来管理要解决的问题,并让每个线程在完成当前线程时从队列中取出另一个线程.当前向每个线程发送10个方法的问题在于,如果前10个问题比后10个问题更快地解决,则第一个线程将处于空闲状态.如果在线程空闲时动态分配,那么所有线程将一直处于忙碌状态(最后除外).

You ought to use some sort of thread-safe blocking queue to manage the problems to be solved, and have each thread take another one from the queue when it's completed the current one. The problem with your current approach of sending 10 to each thread is that if the first 10 get solved more quickly than the next 10, your first thread will sit around idle. If you allocate dynamically when threads are free, you'll have all threads busy all the time (except at the very end).

这篇关于类中具有状态和可变对象的多线程计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆