Vb.net慢速MultiThreading [英] Vb.net slow MultiThreading

查看:87
本文介绍了Vb.net慢速MultiThreading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



好​​吧贝娄是一个小代码,我正在尝试多线程。我在multiThreads上运行10个任务,每个任务需要1秒,在最好的情况下,所有任务都应该在1秒内完成,但是完成所有任务大约需要3秒。此外,它们不是同时完成,而是每秒完成3-4次任务。难道我做错了什么 ?它只是多线程的工作方式。



Hello,

Alright bellow is a small code with which I was experimenting multithreading. I run 10 tasks on multiThreads and each task takes 1second, in the best scenario all task should complete in 1second, but it takes around 3seconds to complete them all. In addition they finish not simultaneously but rather in batch of 3-4 tasks per second. Am I doing something wrong ? it is just the way multithreading works.

Imports System.Threading

Module Module1

    Sub Main()
        Dim ListOfTasks As New List(Of Task)
        Dim watch As New Stopwatch

        watch.Start()

        For i As Integer = 0 To 10
            Console.WriteLine("Started task: " + i.ToString)
            ListOfTasks.Add(task.Factory.StartNew(AddressOf LongTask))
        Next

        Task.WaitAll(ListOfTasks.ToArray)
        watch.Stop()

        Console.WriteLine("All done in: " & watch.ElapsedMilliseconds)
        Console.Read()

    End Sub

    Public Sub LongTask()
        Threading.Thread.Sleep(1000)
        Console.WriteLine("done")
    End Sub
End Module





屏幕截图 http://img833.imageshack.us/img833/4295/81k2.jpg





感谢您的评论



screenshot http://img833.imageshack.us/img833/4295/81k2.jpg


Thank you for your comments

推荐答案

:笑:

为了运行,一个线程需要处理器时间 - 所以如果你有两个任务,那么两个核心就可以在t处继续运行他同时。但是,如果你有四个任务和两个核心,其中只有两个可以同时运行 - 所以其中两个可能会等待一个免费核心才能运行。



在您的情况下,我怀疑您有4个核心,因此任务数量作为批处理完成,然后下一批可以运行。 (三四线可能是因为你的线程并不是系统中唯一运行的线程。)



实际上,它比这更复杂,因为现代Windows使用先发制人的多任务处理方案,而不是等待任务完成,但这是一般原因 - 如果你想要8个任务以真正的并行方式运行,你需要8个核心来运行它们!
:laugh:
In order to run, a thread needs processor time - so if you have two tasks, and two cores then can both proceed at the same time. However, if you have four tasks and two cores, only two of them can run at the same time - so two of them will likely be waiting for a free core before they can run.

In your case, I suspect you have 4 cores, so that number of tasks finish as a batch, then the next bunch can run. (the three-or-four bit is probably because your threads are not the only ones running in the system).

In practice, it's more complex than that, because modern Windows uses a pre-emptive multitasking scheme rather than waiting for a task to complete, but that's the general reason - if you want 8 tasks running in true parallel, you need 8 cores to run them!


我删除了对StopWatch类的调用,添加了开始/停止时间显示并运行它。它跑得很快。有关详细说明,请参见解决方案1。我有4个核心(8个逻辑处理器)。因此,最后三个任务需要更长的时间。



I removed the calls to the StopWatch class, added start/stop time display and ran it. It ran quickly. See Solution 1 for a detailed explanation. I have 4 cores (8 logical processors). Therefore, the last three tasks took a little longer.

Sub Main()
        Dim ListOfTasks As New List(Of Task)
        'Dim watch As New Stopwatch

        ' watch.Start()

        For i As Integer = 0 To 10
            Console.WriteLine("Started task: " & i.ToString & " " & Format(Now, "hh:mm:ss.ffff"))
            ListOfTasks.Add(task.Factory.StartNew(AddressOf LongTask))
        Next

        ' Task.WaitAll(ListOfTasks.ToArray)
        ' watch.Stop()

        'Console.WriteLine("All done in: " & watch.ElapsedMilliseconds)
        'Console.Read()

    End Sub

    Public Sub LongTask()
        Threading.Thread.Sleep(1000)
        Console.WriteLine("done " & Format(Now, "hh:mm:ss.ffff"))
    End Sub







开始任务:0 09:28:52.5484

开始任务:1 09:28:52.5484

开始任务:2 09:28:52.5484

开始任务:3 09:28:52.5484

开始任务:4 09:28:52.5484

开始任务:5 09:28:52.5484

开始任务:6 09: 28:52.5484

开始任务:7 09:28:52.5484

开始任务:8 09:28:52.5484

开始任务:9 09 :28:52.5484

开始任务:10 09:28:52.5484

完成09:28:53.5624

done 09:28:53.5624

完成09:28:53.5624

完成09:28:53.5624

完成09:28:53.5624

完成09:28:53.5624

done 09:28:53.5624

done 09:28:53.5780

done 09:28:54.5764
完成09:28:54.5764

完成09:28:54.5764




Started task: 0 09:28:52.5484
Started task: 1 09:28:52.5484
Started task: 2 09:28:52.5484
Started task: 3 09:28:52.5484
Started task: 4 09:28:52.5484
Started task: 5 09:28:52.5484
Started task: 6 09:28:52.5484
Started task: 7 09:28:52.5484
Started task: 8 09:28:52.5484
Started task: 9 09:28:52.5484
Started task: 10 09:28:52.5484
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5624
done 09:28:53.5780
done 09:28:54.5764
done 09:28:54.5764
done 09:28:54.5764


这篇关于Vb.net慢速MultiThreading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆