Threading: does c# have an equivalent of the Java Runnable interface?
Solution 1
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)..
Solution 2
Does c# have an equivalent of the Java Runnable interface?
Yes, it's 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
class Runner implements Runnable {
void someMethod() {
Thread newThread = new Thread( this );
newThread.start();
}
public void run() {
out.println("Running in a different thread.");
}
}
Solution 3
The ThreadStart
delegate is 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.
MSDN explains about delegates:
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.
Solution 4
C#
uses the ThreadStart
delegate instead of Java's Runnable
style.
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();
}
}
Solution 5
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.
DiggerMeUp
Updated on January 30, 2020Comments
-
DiggerMeUp over 4 years
Does c# have an equivalent of the Java Runnable interface?
If not how could this be implemented or is it simply not needed?
thanks.
-
Powerlord over 14 yearsSide note: in Java, the
Runnable
interface is an alternative to subclassingThread
, but you still have to create a newThread
object, passing theRunnable
to a constructor. -
Michael Borgwardt over 14 yearsSo basically, ThreadStart is the exact C# equivalent to Java's Runnable.
-
OscarRyz over 14 years@Michel Borgwardt: Yeap, the answer is correct ( in essence ) but wrong in the way is it written.
-
codeMonkey almost 9 yearsAs of 2015-09-25 this has yet to be implemented for Xamarin, so you're stuck with Frankenstein C#/Java options to accomplish the same thing as this answer.
-
Gordon Milne over 5 yearsRunnable has NOTHING to do with threads. The way people use it has a LOT to do with threads.
-
Gordon Milne over 5 yearsRunnable, in and of itself, has NOTHING to do with threads. The way people use it has a LOT to do with threads. Runnable is an interface that defines a void run method. There is nothing about Runnable that requires threads be involved. You can, for example, have a set of observers on an object which are nothing but Runnables that get their run() method invoked every time the observer needs calling. I know this may seem a tad contrived but, for a large number of use cases, it is exactly what you need.