线程私有列表但与方法共享
A thread private list but shared with methods
我需要启动多个线程,但每个线程都有自己的列表。这意味着,列表不会被共享,但它们将通过不同的方法访问。
class foo()
{
//put the list definition here as a property?
public main()
{
launchthread(threadMethod1)
}
public void threadMethod1()
{
// create a list instance here?
// do something about the list, modify data in it
threadMethod2();
}
public void threadMethod2()
{
// do something about list, modify data in it
}
}
我应该如何正确定义列表,将定义放在哪里?如果我只想有一个线程私有列表,它与线程安全有什么关系吗?
除了将列表作为 threadmethod2
.
的参数传递之外,我正在寻找其他选项
假设我不需要使用 ConcurrentBag/SynchronizedCollection
或担心锁的东西?
我认为开发多线程解决方案需要考虑几件事
需要一个主线程以防将来需要线程处理程序,例如监视线程状态。下面是一个基本的主线程。
public MasterThread
{
private ConcurrentBag<MyThreadImplementation> threadList;
public MasterThread()
{
threadList = new ConcurrentBag<MyThreadImplementation>();
}
public void LaunchThread(MyThreadImplementation newThread)
{
threadList.Add(newThread);
newThread.Start();
}
}
注意 MyThreadImplementation 将是一个接口或抽象 class,它定义了您要实现的所有线程的共享方法,或者如果您不需要具体的实现或逻辑,则为 Thread class在你的线程中。我建议
下面的摘要 class
public abstract class MyThreadImplememtation : Thread
{
private List<YourObject> privateList;
public MyThreadImplementation()
{
list = new ArrayList<MyObject>();
}
// it will be executed when Thread.start() is called
public void run()
{
CommonMethod();
ConcreteMethod();
}
public void CommonMethod()
{
// common method to all threads, it can manipulate the private list
}
// abstract method to be implemented by the children clases
public abstract void ConcreteMethod();
}
请注意,您的所有线程都将从 MyThreadImplementation 继承并实现自己的逻辑,每个线程都有自己的私有列表和逻辑,除了对所有线程都相同的 CommonMethod 之外。此外,线程的主要逻辑将 运行 首先是 CommonMethod,然后是 ConcreteMethod。
线程示例:
public class WhateverThread : MyThreadImplementation
{
public override void ConcreteMethod()
{
// manipulate the private list
list.Add(new YourObject());
}
}
我说的对吗,你打算像这样开始你的话题:
class Foo<T>()
{
List<T> TLS;
public main()
{
launchThread(threadMethod1)
}
public void threadMethod1()
{
TLS = new List<T>()
// TLS.Add(); HERE
threadMethod2();
}
public void threadMethod2()
{
// TLS.Add(); HERE
}
}
如果是这样,那么它对您来说是完全线程安全的,并且您不需要在 foo
定义中的 ConcurrentCollection
-class 中,因为只有一个线程将访问这个变量。您不需要将列表添加为参数,因为 threadMethod1
或 threadMethod2
应该是实例方法,并且它们内部的代码将看到与 TLS
相同的引用变量。
我想指出,您没有使用 C# 的默认编码标准 - 例如,class 名称和方法名称不在 CamelCasing 中。我认为您的代码需要正确格式化。
我还想注意 threadMethod1
或 threadMethod2
不应该是 public
因为它们仅从 class 内部调用,您应该限制使用它们的其他 classes(如果我很了解您的体系结构)。
Good reference about TLS
(thread-local-storage) from comments.
我需要启动多个线程,但每个线程都有自己的列表。这意味着,列表不会被共享,但它们将通过不同的方法访问。
class foo()
{
//put the list definition here as a property?
public main()
{
launchthread(threadMethod1)
}
public void threadMethod1()
{
// create a list instance here?
// do something about the list, modify data in it
threadMethod2();
}
public void threadMethod2()
{
// do something about list, modify data in it
}
}
我应该如何正确定义列表,将定义放在哪里?如果我只想有一个线程私有列表,它与线程安全有什么关系吗?
除了将列表作为 threadmethod2
.
假设我不需要使用 ConcurrentBag/SynchronizedCollection
或担心锁的东西?
我认为开发多线程解决方案需要考虑几件事
需要一个主线程以防将来需要线程处理程序,例如监视线程状态。下面是一个基本的主线程。
public MasterThread
{
private ConcurrentBag<MyThreadImplementation> threadList;
public MasterThread()
{
threadList = new ConcurrentBag<MyThreadImplementation>();
}
public void LaunchThread(MyThreadImplementation newThread)
{
threadList.Add(newThread);
newThread.Start();
}
}
注意 MyThreadImplementation 将是一个接口或抽象 class,它定义了您要实现的所有线程的共享方法,或者如果您不需要具体的实现或逻辑,则为 Thread class在你的线程中。我建议
下面的摘要 classpublic abstract class MyThreadImplememtation : Thread
{
private List<YourObject> privateList;
public MyThreadImplementation()
{
list = new ArrayList<MyObject>();
}
// it will be executed when Thread.start() is called
public void run()
{
CommonMethod();
ConcreteMethod();
}
public void CommonMethod()
{
// common method to all threads, it can manipulate the private list
}
// abstract method to be implemented by the children clases
public abstract void ConcreteMethod();
}
请注意,您的所有线程都将从 MyThreadImplementation 继承并实现自己的逻辑,每个线程都有自己的私有列表和逻辑,除了对所有线程都相同的 CommonMethod 之外。此外,线程的主要逻辑将 运行 首先是 CommonMethod,然后是 ConcreteMethod。
线程示例:
public class WhateverThread : MyThreadImplementation
{
public override void ConcreteMethod()
{
// manipulate the private list
list.Add(new YourObject());
}
}
我说的对吗,你打算像这样开始你的话题:
class Foo<T>()
{
List<T> TLS;
public main()
{
launchThread(threadMethod1)
}
public void threadMethod1()
{
TLS = new List<T>()
// TLS.Add(); HERE
threadMethod2();
}
public void threadMethod2()
{
// TLS.Add(); HERE
}
}
如果是这样,那么它对您来说是完全线程安全的,并且您不需要在 foo
定义中的 ConcurrentCollection
-class 中,因为只有一个线程将访问这个变量。您不需要将列表添加为参数,因为 threadMethod1
或 threadMethod2
应该是实例方法,并且它们内部的代码将看到与 TLS
相同的引用变量。
我想指出,您没有使用 C# 的默认编码标准 - 例如,class 名称和方法名称不在 CamelCasing 中。我认为您的代码需要正确格式化。
我还想注意 threadMethod1
或 threadMethod2
不应该是 public
因为它们仅从 class 内部调用,您应该限制使用它们的其他 classes(如果我很了解您的体系结构)。
Good reference about TLS
(thread-local-storage) from comments.