线程:c# 是否具有 Java Runnable 接口的等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1923512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Threading: does c# have an equivalent of the Java Runnable interface?
提问by DiggerMeUp
Does c# have an equivalent of the Java Runnable interface?
c# 是否具有等效于 Java Runnable 的接口?
If not how could this be implemented or is it simply not needed?
如果不是,如何实现或者根本不需要它?
thanks.
谢谢。
采纳答案by Fergal Moran
Nope. C# handles threads differently to Java. Rather than subclassing the Thread class, you simply create a new System.Threading.Thread object and pass it a ThreadStart delegate (this is the function where you do the work)..
不。C# 处理线程的方式与 Java 不同。而不是继承 Thread 类,您只需创建一个新的 System.Threading.Thread 对象并将其传递给 ThreadStart 委托(这是您执行工作的函数)。
回答by Lee
It's not needed - threads in C# take an instance of a ThreadStart
or ParameterizedThreadStart
delegate which are the runnable components of the thread to execute.
不需要 - C# 中的线程采用 aThreadStart
或ParameterizedThreadStart
委托的实例,它们是要执行的线程的可运行组件。
回答by Florian Doyon
.Net uses the ThreadStart
and ParameterizedThreadStart
delegates to bootstrap Threads.
.Net 使用ThreadStart
和ParameterizedThreadStart
委托来引导线程。
Delegates being first-class citizens in .Net, you can keep a reference around if you need to.
代表是 .Net 中的一等公民,如果需要,您可以保留参考。
回答by Sam Harwell
The closest to a high level task-oriented threading API would be a BackgroundWorker
. As others have mentioned, .NET (and thus C#) use delegates for representing a callable method. Java doesn't have that concept (function pointers), and instead uses interfaces for callable objects.
最接近高级面向任务的线程 API 是BackgroundWorker
. 正如其他人所提到的,.NET(以及 C#)使用委托来表示可调用方法。Java 没有这个概念(函数指针),而是使用可调用对象的接口。
回答by Daniel Pryden
The ThreadStart
delegateis essentially the same as the Runnable
interface. A delegate is like an interface for a single method rather than an entire class, so it's actually easier to implement than the Runnable
interface in Java.
该ThreadStart
委托是基本相同的Runnable
接口。委托就像是单个方法的接口而不是整个类,因此它实际上比Runnable
Java 中的接口更容易实现。
MSDN explains about delegates:
MSDN解释了委托:
Delegates and interfaces are similar in that they enable the separation of specification and implementation. Multiple independent authors can produce implementations that are compatible with an interface specification. Similarly, a delegate specifies the signature of a method, and authors can write methods that are compatible with the delegate specification. When should you use interfaces, and when should you use delegates?
Delegates are useful when:
- A single method is being called.
- A class may want to have multiple implementations of the method specification.
- It is desirable to allow using a static method to implement the specification.
- An event-like design pattern is desired (for more information, see the Events Tutorial).
- The caller has no need to know or obtain the object that the method is defined on.
- The provider of the implementation wants to "hand out" the implementation of the specification to only a few select components.
- Easy composition is desired.
Interfaces are useful when:
- The specification defines a set of related methods that will be called.
- A class typically implements the specification only once.
- The caller of the interface wants to cast to or from the interface type to obtain other interfaces or classes.
委托和接口的相似之处在于它们支持规范和实现的分离。多个独立作者可以生成与接口规范兼容的实现。同样,委托指定方法的签名,作者可以编写与委托规范兼容的方法。什么时候应该使用接口,什么时候应该使用委托?
委托在以下情况下很有用:
- 正在调用一个方法。
- 一个类可能需要方法规范的多个实现。
- 允许使用静态方法来实现规范是可取的。
- 需要类似事件的设计模式(有关更多信息,请参阅事件教程)。
- 调用者无需知道或获取定义该方法的对象。
- 实现的提供者希望将规范的实现“分发”给少数几个选定的组件。
- 需要简单的组合。
接口在以下情况下很有用:
- 该规范定义了一组将被调用的相关方法。
- 一个类通常只实现一次规范。
- 接口的调用者想要从接口类型转换为或从接口类型转换以获得其他接口或类。
回答by Dave Sims
C#
uses the ThreadStart
delegate instead of Java's Runnable
style.
C#
使用ThreadStart
委托而不是 Java 的Runnable
风格。
public class Foo
{
public void DoStuff()
{
while (true)
{
// do some stuff
}
}
};
public class Bar
{
public static int Main()
{
Foo foo = new Foo();
// create a ThreadStart delegate and pass in the method that will run
// (similar to run on Java's Runnable)
Thread thread = new Thread(new ThreadStart(foo.DoStuff));
thread.Start();
}
}
回答by OscarRyz
Does c# have an equivalent of the Java Runnable interface?
c# 是否具有等效于 Java Runnable 的接口?
Yes, it's ThreadStart
是的,它是ThreadStart
class Runner
{
void SomeMethod()
{
Thread newThread = new Thread(new ThreadStart(Run));
newThread.Start();
}
public void Run()
{
Console.WriteLine("Running in a different thread.")
}
}
Would be equivalent to the following Java code
将等效于以下 Java 代码
class Runner implements Runnable {
void someMethod() {
Thread newThread = new Thread( this );
newThread.start();
}
public void run() {
out.println("Running in a different thread.");
}
}