找到你要的答案

Q:Is using locking mechanism in Threading program equivalent to executing the code synchronously rather then asynchronously?

Q:在线程程序中使用锁定机制等同于同步执行代码而不是异步吗?

Example code below uses the concept of locking. But that prevents it from giving parallel execution. So is the title of the question correct: Is using locking mechanism in Threading program equivalent to executing the code synchronously rather then asynchronously?

Does same case apply for Threading.Monitor and declaring class with [Synchronization] Attribute ??

static void Main(string[] args) 
{    
    Console.WriteLine("*****Synchronizing Threads *****\n");  
    Printer p = new Printer();  
    // Make 10 threads that are all pointing to the same    
    // method on the same object.    
    Thread[] threads = new Thread[10]; 
    for (int i = 0; i < 10; i++)    
    {      
        threads[i] = new Thread(new ThreadStart(p.PrintNumbers));  
        threads[i].Name = string.Format("Worker thread #{0}", i);    
    }  
    // Now start each one.   
    foreach (Thread t in threads)  
    t.Start();   
    Console.ReadLine();
} 

public void PrintNumbers() 
{   
    // Use the private object lock token.  
    lock (threadLock)  
    {    
        // Display Thread info.
        Console.WriteLine("-> {0} is executing PrintNumbers()",Thread.CurrentThread.Name);  
        // Print out numbers.    
        Console.Write("Your numbers: "); 
        for (int i = 0; i < 10; i++)  
        {      
            Random r = new Random();   
            Thread.Sleep(1000 * r.Next(5));  
            Console.Write("{0}, ", i);    
        }     
        Console.WriteLine();  
    }
}

Example code below uses the concept of locking. But that prevents it from giving parallel execution. So is the title of the question correct: 在线程程序中使用锁定机制等同于同步执行代码而不是异步吗?

同样的情况是否适用于线程。用“同步”属性监视和声明类??

static void Main(string[] args) 
{    
    Console.WriteLine("*****Synchronizing Threads *****\n");  
    Printer p = new Printer();  
    // Make 10 threads that are all pointing to the same    
    // method on the same object.    
    Thread[] threads = new Thread[10]; 
    for (int i = 0; i < 10; i++)    
    {      
        threads[i] = new Thread(new ThreadStart(p.PrintNumbers));  
        threads[i].Name = string.Format("Worker thread #{0}", i);    
    }  
    // Now start each one.   
    foreach (Thread t in threads)  
    t.Start();   
    Console.ReadLine();
} 

public void PrintNumbers() 
{   
    // Use the private object lock token.  
    lock (threadLock)  
    {    
        // Display Thread info.
        Console.WriteLine("-> {0} is executing PrintNumbers()",Thread.CurrentThread.Name);  
        // Print out numbers.    
        Console.Write("Your numbers: "); 
        for (int i = 0; i < 10; i++)  
        {      
            Random r = new Random();   
            Thread.Sleep(1000 * r.Next(5));  
            Console.Write("{0}, ", i);    
        }     
        Console.WriteLine();  
    }
}
answer1: 回答1:

Is using locking mechanism in Threading program equivalent to executing the code synchronously rather then asynchronously?

Specifically in your example, you're wrapping the entire method call with a lock, so you'll experience synchronous behavior, and threading in this case would be useless and provide more overhead than gain.

Many times, you have a specific place where locking is needed when accessing shared state, and that is usually where the limit of your parallelism lays. But you may still gain computational benefits if you have a significant amount of CPU work which can be executed in parallel.

As a side note, there is a difference between asynchrony and parallalism. I'd advise you to read this article for more on the meaning of both.

在线程程序中使用锁定机制等同于同步执行代码而不是异步吗?

特别是在你的例子中,你用一个锁封装整个方法调用,所以你将体验同步行为,并且线程在这种情况下是无用的,提供更多的开销比增益。

很多时候,你有一个特定的地方,在访问共享状态时需要锁定,这通常是你的平行度限制。但是,如果你有大量的CPU工作可以并行执行,那么你仍然可以获得计算效益。

作为一个方面说明,有parallalism同步性的差异。我建议你阅读这篇文章,以了解两者的含义。

answer2: 回答2:

Is using locking mechanism in Threading program equivalent to executing the code synchronously rather then asynchronously?

It combines the lack of concurrency of a synchronous program with the overheads of multi-threading to combine the worse parts of both approaches.

It is though just to demonstrate that locking forces synchronised access to particular bits of code. In real multi-threaded use one holds locks for as short a time as possible, so different threads can work simultaneously and only block each other when absolutely necessary.

在线程程序中使用锁定机制等同于同步执行代码而不是异步吗?

它结合了同步程序的并发性与多线程的开销相结合,这两种方法的更糟糕的部分缺乏。

虽然只是表明锁定力同步访问特定代码位。在真正的多线程使用一个持有锁尽可能短的时间,所以不同的线程可以同时工作,只在必要时互相阻塞。

c#  locking