本文介绍了有关线程的问题...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习多线程.我有一本书(考试70-536自定进度的培训工具包),其中提供了有关多线程的示例.
因此,我尝试了这一点(摘自本书).代码如下:

I am currently learning about multithreading. I have a book (Exam 70-536 self-paced training kit) which gives an example about multi-threading.
So I have tried this (taken from the book). The code is as follows:

Imports System.Threading

Public Class FastMemFile
    Private _file As String = "Hello, World!"
    Private _rwl As New ReaderWriterLock

    Public Sub ReadFile()
        _rwl.AcquireReaderLock(10000)
        For i As Integer = 1 To 3
            Console.WriteLine(_file)
            Thread.Sleep(1000)
        Next
        _rwl.ReleaseReaderLock()
    End Sub

    Public Sub WriteFile()
        _rwl.AcquireWriterLock(10000)
        _file += " It's a nice day!"
        _rwl.ReleaseWriterLock()
    End Sub

End Class

Public Class SlowMemFile
    Private _file As String = "Hello, World!"

    Public Sub ReadFile()
        SyncLock Me
            For i As Integer = 1 To 3
                Console.WriteLine(_file)
                Thread.Sleep(1000)
            Next
        End SyncLock
    End Sub

    Public Sub WriteFile()
        SyncLock Me
            _file += " It's a nice day!"
        End SyncLock
    End Sub

End Class



如您所见,这些类的功能完全相同,除了FastMemFile使用ReaderWriterLock和SlowMemFile使用SyncLock.调用代码如下所示:



As you see the classes do exactly the same, except that the FastMemFile uses a ReaderWriterLock and the SlowMemFile uses SyncLock. The calling code looks like this:

Imports System.Threading

Module Module1

    Sub Main()
        Dim m As New SlowMemFile ' / FastMemFile

        Dim t1 As New Thread(New ThreadStart(AddressOf m.ReadFile))
        Dim t2 As New Thread(New ThreadStart(AddressOf m.WriteFile))
        Dim t3 As New Thread(New ThreadStart(AddressOf m.ReadFile))
        Dim t4 As New Thread(New ThreadStart(AddressOf m.ReadFile))

        t1.Start()
        t2.Start()
        t3.Start()
        t4.Start()
    End Sub

End Module


实际上,FastMemFile比SlowMemFile快得多.但是,我得到的结果似乎是完全随机的(对于两个类).有时我会收到你好,世界!" 9次,有时我会收到"Hello,World!真是美好的一天" 9次,有时我会收到"Hello,World!" 3次,今天真好!" 6次,还有其他次......
这与我拥有超快速的Intel Core i7有什么关系吗?
如果是这样,那么我如何确保我的代码确实以正确的顺序执行,但是要在单独的线程上执行?我想确保无论处理器速度如何,代码都能正确执行.
感谢一些代码示例,但实际上更欢迎一些解决该问题的好文章.


The FastMemFile is indeed much faster than the SlowMemFile. However the results I get seems to be completely random (for both classes). Sometimes I get "Hello, World!" 9 times, sometimes I get "Hello, World! It''s a nice day" 9 times, and sometimes I get "Hello, World!" 3 times and "It''s a nice day!" 6 times and yet other times it''s the other way around...
Does this have anything to do with me having an ultra-fast Intel Core i7?
And if this is so then how can I make sure my code does actually execute in the right order, but on seperate threads? I want to make sure the code executes correctly no matter the processor speed.
Some code examples are appreciated, but some good articles that address the problem are actually more welcome.

推荐答案


public class ThreadTest
{
    private string _file="Hello World";
    private ReaderWriterLock _rw1=new ReaderWriterLock();
    public void ReadFile(object obj)
    {
        _rw1.AcquireReaderLock(1000);
        for(int i=0;i<10;i++)
        {
            if ((int)obj == 1)
            {
                Thread.Sleep(10000);
            }
            else
            {
                Thread.Sleep(1000);
            }
        }
           System.Windows.Forms.MessageBox.Show(_file+" is accessed by "+obj.ToString());
        _rw1.ReleaseReaderLock();
    }
}




然后使用
称呼它




Then call this using

ThreadTest t = new ThreadTest();
Thread th1=new Thread(new ParameterizedThreadStart(t.ReadFile));
Thread th2 = new Thread(new ParameterizedThreadStart(t.ReadFile));

th1.Start(1);
th2.Start(2);



在这里,th1首先被调用.您是否希望th2必须等到th1完成工作?就像它调用的顺序一样.



Here the th1 is called first. Do you expect th2 has to wait till th1 finishes the job? as the order it called.


这篇关于有关线程的问题...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:31