Multi threading issues, established 6 threads, would be the 6 threads run mechan

The following code through the for statement to create multiple threads, each thread is executing down process.
The following code would be several threads should execute the process of down code at the same time, but the actual operation, but only two thread is running, the two thread after the implementation, after the two thread automatically and then run.

Such as through the establishment of for 6 threads, the first thread 0 and thread 1 simultaneous execution of down process, the two thread after the implementation, it is the 2 and the 3 thread two thread running down process...

        Public Sub Start()
            Dim request As HttpWebRequest = DirectCast(WebRequest.Create(_fileUrl), HttpWebRequest)
            Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            _extName = response.ResponseUri.ToString().Substring(response.ResponseUri.ToString().LastIndexOf("."c))
            'Get real extension
            _fileSize = response.ContentLength
            Dim singelNum As Integer = CInt(_fileSize \ _threadNum)
            'The average allocation
            Dim remainder As Integer = CInt(_fileSize Mod _threadNum)
            'Gets the remaining
            For i As Integer = 0 To _threadNum - 1   'Assume that the _threadNum value is 6, the for cycle is operating normally, the execution of the 6. 
                Dim range As New List(Of Integer)()
                range.Add(i * singelNum)
                If remainder <> 0 AndAlso (_threadNum - 1) = i Then
                    'The remaining gave the last thread
                    range.Add(i * singelNum + singelNum + remainder - 1)
                    range.Add(i * singelNum + singelNum - 1)
                End If
                _thread(i) = New Thread(Sub()
                                                Download(range(0), range(1))
                                            Catch ex As Exception
                                                ' Throw New Exception(ex.Message)
                                                Debug.Print("Error: "& ex.Message &" thread name:" & Thread.CurrentThread.Name)
                                            End Try

                                        End Sub)

                _thread(i).Name = String.Format("temp_{0}", i + 1)
        End Sub

        Private Sub Download(ByVal from As Integer, ByVal [to] As Integer)
            Dim httpFileStream As Stream = Nothing, localFileStram As Stream = Nothing
                Dim tmpFileBlock As String = String.Format("{0}\{1}_{2}.dat", _savePath, _fileName, Thread.CurrentThread.Name)
                Dim httprequest As HttpWebRequest = DirectCast(WebRequest.Create(_fileUrl), HttpWebRequest)
                httprequest.AddRange(from, [to])
                Dim httpresponse As HttpWebResponse = DirectCast(httprequest.GetResponse(), HttpWebResponse)
                httpFileStream = httpresponse.GetResponseStream()
                localFileStram = New FileStream(tmpFileBlock, FileMode.Create)
                Dim by As Byte() = New Byte(4999) {}
                Dim getByteSize As Integer = httpFileStream.Read(by, 0, CInt(by.Length))
                'The Read method returns the total number of bytes read into the by variable.
                While getByteSize > 0
                    SyncLock locker
                        _downloadSize += getByteSize
                    End SyncLock
                    localFileStram.Write(by, 0, getByteSize)
                    getByteSize = httpFileStream.Read(by, 0, CInt(by.Length))
                End While
                SyncLock locker
                    _threadCompleteNum += 1
                End SyncLock
            Catch ex As Exception
                Throw New Exception(ex.Message.ToString())
                If httpFileStream IsNot Nothing Then
                End If
                If localFileStram IsNot Nothing Then
                End If
            End Try
            If _threadCompleteNum = _threadNum Then
                _isComplete = True
            End If
        End Sub

Started by Tyler at November 15, 2016 - 6:53 PM

VB don't understand, you Cpu supports 6 threads? It is not possible to do 6 thread real synchronous

Posted by Shelley at November 27, 2016 - 7:18 PM