简体   繁体   English

线程化:c#是否具有Java Runnable接口的等价物?

[英]Threading: does c# have an equivalent of the Java Runnable interface?

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. 谢谢。

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.");
      }
  }

Nope. 不。 C# handles threads differently to Java. C#以不同于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).. 您只需创建一个新的System.Threading.Thread对象并将其传递给ThreadStart委托(这是您完成工作的函数),而不是继承Thread类。

The ThreadStart delegate is essentially the same as the Runnable interface. ThreadStart委托Runnable接口基本相同。 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. 委托就像是单个方法而不是整个类的接口,因此它实际上比Java中的Runnable接口更容易实现。

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. 接口的调用者想要转换为接口类型或从接口类型转换为获取其他接口或类。

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();
    }
}

它不是必需的 - C#中的线程采用ThreadStartParameterizedThreadStart委托的实例,它们是要执行的线程的可运行组件。

The closest to a high level task-oriented threading API would be a BackgroundWorker . 最接近高级面向任务的线程API将是BackgroundWorker As others have mentioned, .NET (and thus C#) use delegates for representing a callable method. 正如其他人所提到的,.NET(以及C#)使用委托来表示可调用方法。 Java doesn't have that concept (function pointers), and instead uses interfaces for callable objects. Java没有那个概念(函数指针),而是使用可调用对象的接口。

.Net uses the ThreadStart and ParameterizedThreadStart delegates to bootstrap Threads. .Net使用ThreadStartParameterizedThreadStart委托来引导线程。

Delegates being first-class citizens in .Net, you can keep a reference around if you need to. 代表作为.Net的一等公民,如果需要,您可以随时参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM