简体   繁体   中英

How to perform Java or Qt Like MultiThread Programming in C#

You know that multi-thread programming is of great importance for us to develop something more efficiently.

And there is common structure for multi-threading concept in Java, Qt or ACE They provide us a common interface to implement such as void run() method, Mutex and Semaphore.

And C# has some kind of multi-thread facilities. But If a developer like me want to use so-called structure,what should he/she do? There is a any library or something?

In C# there's Mutex , Semaphore , Threading , Parallel LINQ , Async Await and a whole lot of other techniques you can use.

It all depends on the context. "What do you want to do" determines what tools you'd want to use.

Edit:
Javas Runnable interface can easily be mimiced in C# (examples from this paper):

Java :

public class Counter implements Runnable { 
  private int count; 
  public Counter(int val) { this.count = val; } 
  public void run() { 
    for(int i=0; i < count; i++) 
    System.out.println(“Hello World”); 
  } 
  public static void main(String args[]) { 
    Counter c = new Counter(10); 
    Thread t = new Thread(c); 
    t.start(); 
  } 
}

C# Equivalent:

using System; 
using System.Threading; 

namespace SimpleThreadExample { 
  class Counter { 
    private int count; 
    public Counter(int val) { this.count = val; } 
    public void DoCount() { 
      for(int i=0; i < count; i++) 
        System.Console.WriteLine(“Hello World”); 
    } 
    [STAThread] 
    static void Main(String[] args) { 
      Counter c = new Counter(10); 
      Thread t = new Thread(new ThreadStart(c.DoCount)); 
      t.start(); 
    } 
  } 
}

Most modern language packages come with libraries that allow access to the OS calls that manage additional threads inside a process. Pick one and have a go. Ask again when you have something that you cannot get to work correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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