multi threading programming

G

Guoqi Zheng

Dear sir,

I am writing an application to process log file. Log files of many of our site are often huge every day. I need the application to process them very quick. That is why I am thinking to use multi thread.

I am not very familiar with this, does some one has a tutorial for this. I need something like..

Sub Main()

' process text file, catch something
SubMultiThread(strSomething)


End sub

Sub SubMultiThread(ByVal strInput as string)

' do some task using multi thread. It can open new thread. If all threads are in use, it will wait.

End sub


Thanks in advanced.
 
A

Alvin Bruney [MVP]

There's lots of tutorials sprinkled on the web for that. Google for
threadpool and you should find a relevant example.
Dear sir,
we have luvely ladies in here too...

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 
A

adebaene

Guoqi said:
Dear sir,

I am writing an application to process log file. Log files of many of
our site are often huge every day. I need the application to process
them very quick. That is why I am thinking to use multi thread.

Note that if the work yo have to do is processor intensive (ie, most of
the time is spent on actual computing, not waiting for some I/O),
multithreading won't help you get any faster unless you've got a
multi-processor system (or an hyperthread / multicore processor).
I am not very familiar with this, does some one has a tutorial for this. I need something like..

Sub Main()

' process text file, catch something
SubMultiThread(strSomething)


End sub

Sub SubMultiThread(ByVal strInput as string)

' do some task using multi thread. It can open new thread. If all
threads are in use, it will wait.

Looks like the ThreadPool class may be a godd starting point for you.

Arnaud
MVP - VC
 
A

Alvin Bruney [MVP]

before you going *trying it out, be sure to read up on threading on MSDN. It
will save you some asprin.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://www.lulu.com/owc
 
J

Jon Skeet [C# MVP]

Guoqi Zheng said:
I am writing an application to process log file. Log files of many of
our site are often huge every day. I need the application to process
them very quick. That is why I am thinking to use multi thread.

Why do you think that multi-threading would help you? It doesn't sound
like the kind of task which is actually helped much by multi-threading.
Chances are you'll be limited by disk access more than anything else,
and multi-threading that is likely to make it worse rather than better.

See http://www.pobox.com/~skeet/csharp/threads/whentouse.shtml
 
P

Pawel Janowski

Jon said:
Why do you think that multi-threading would help you? It doesn't sound
like the kind of task which is actually helped much by multi-threading.
Chances are you'll be limited by disk access more than anything else,
and multi-threading that is likely to make it worse rather than better.

See http://www.pobox.com/~skeet/csharp/threads/whentouse.shtml

==================== mergesort ===============================

Imports System
Imports System.Collections
Imports System.IO
Imports System.Threading



Module MergeSortApp

Sub Main()



Dim czas As DateTime
Dim roznica As String

Console.Clear()
Console.WriteLine("MergeSort VB.NET - wersja wielowÄ…tkowa")
Console.WriteLine("")
Console.WriteLine("Paweł Janowski - 9875")
Console.WriteLine()

' pobieranie nazw plików
Dim szSrcFile, szDestFile As String
szSrcFile = "miasta_unsorted.txt"
szDestFile = "miasta_sorted.txt"

' odczyt pliku źródłowego
Dim szSrcLine As String
Dim szContents As ArrayList
Dim fsInput As FileStream
Dim srInput As StreamReader
szContents = New ArrayList()
Try
fsInput = New FileStream(szSrcFile, FileMode.Open,
FileAccess.Read)
Catch ex As Exception
Console.WriteLine("nie moge odczytać pliku źródłowego")
Exit Sub
End Try

srInput = New StreamReader(fsInput)
szSrcLine = srInput.ReadLine()
While Not IsNothing(szSrcLine)
' tworzenie tablicy
szContents.Add(szSrcLine)
szSrcLine = srInput.ReadLine()
End While
srInput.Close()
fsInput.Close()

'sortowanie
dim oMergeSortClass as new MergeSortClass
dim t as new Thread(AddressOf oMergeSortClass.MergeSort)
oMergeSortClass.szArray = szContents
oMergeSortClass.nLower = 0
oMergeSortClass.nUpper = szContents.Count - 1
Console.ReadKey()
czas = Now()
t.Start()
t.Join()
roznica = dateDiff("s", czas, Now()).ToString

' zapisywanie posortowanych linijek
Dim fsOutput As FileStream
Dim srOutput As StreamWriter
Dim nIndex As Integer
Try
fsOutput = New FileStream(szDestFile, FileMode.Create,
FileAccess.Write)
Catch ex As Exception
Console.WriteLine("nie moge zapisać do pliku docelowego")
Exit Sub
End Try

srOutput = New StreamWriter(fsOutput)
For nIndex = 0 To szContents.Count - 1
' zapis po linijce
srOutput.WriteLine(szContents(nIndex))
Next nIndex
srOutput.Close()
fsOutput.Close()


Console.WriteLine()
Console.WriteLine("Posortowane linie zostały zapisane do pliku
wynikowego")
Console.WriteLine()
Console.WriteLine("Całość trwała " & roznica & " sekund")

Console.ReadKey()
end

End Sub

Class MergeSortClass

Public Event ThreadComplete(ByVal szArray As ArrayList)

Public szArray As ArrayList
Public nLower As Integer
Public nUpper as Integer

Sub MergeSort()




System.Threading.Thread.Sleep(25)
Console.Write("+")


Dim szSwap As String
If (nUpper - nLower) = 1 Then


If szArray(nLower).CompareTo(szArray(nUpper)) > 0 Then
szSwap = szArray(nLower)
szArray(nLower) = szArray(nUpper)
szArray(nUpper) = szSwap
End If

ElseIf (nUpper - nLower) > 1 Then


dim oMergeSortClass1 as new MergeSortClass
dim t1 as new Thread(AddressOf oMergeSortClass1.MergeSort)

oMergeSortClass1.szArray = szArray
oMergeSortClass1.nLower = nLower
oMergeSortClass1.nUpper = (nLower + nUpper) / 2
t1.Start()
t1.Join()


dim oMergeSortClass2 as new MergeSortClass
dim t2 as new Thread(AddressOf oMergeSortClass2.MergeSort)

oMergeSortClass2.szArray = szArray
oMergeSortClass2.nLower = (nLower + nUpper) / 2 + 1
oMergeSortClass2.nUpper = nUpper
t2.Start()
t2.Join()


Dim oMergeClass As New MergeClass
oMergeClass.szArray = szArray
oMergeClass.nLower = nLower
oMergeClass.nMiddle = (nLower + nUpper) / 2 + 1
oMergeClass.nUpper = nUpper
Dim t As New Thread(AddressOf oMergeClass.Merge)
t.Start()
t.Join()
End If

RaiseEvent ThreadComplete(szArray)



End Sub

end Class

Class MergeClass



Public Event ThreadComplete(ByVal szArray As ArrayList)


Public szArray As ArrayList
Public nLower As Integer
Public nMiddle As Integer
Public nUpper

Sub Merge()

System.Threading.Thread.Sleep(25)
Console.Write("-")


Dim nIndex As Integer
Dim szBuffer As New ArrayList()


Dim i, j As Integer
i = nLower
j = nMiddle
While i < nMiddle And j <= nUpper


If (szArray(i).CompareTo(szArray(j)) < 0) Then
szBuffer.Add(szArray(i))
i = i + 1
Else
szBuffer.Add(szArray(j))
j = j + 1
End If

End While


While i < nMiddle
szBuffer.Add(szArray(i))
i = i + 1
End While
While j <= nUpper
szBuffer.Add(szArray(j))
j = j + 1
End While


nIndex = 0
For i = nLower To nUpper
szArray(i) = szBuffer(nIndex)
nIndex = nIndex + 1
Next i


RaiseEvent ThreadComplete(szArray)

End Sub


End Class

End Module



==================== mergesort ===============================

miasta_unsorted.txt (input file)
miasta_sorted.txt (output file)

please send me Ur's opinion.

w/regards
Paul
 
J

Jon Skeet [C# MVP]

please send me Ur's opinion.

Sure - you're always calling Join on a thread you've just started,
which completely removes all the benefits of multi-threading.
Effectively, your code is single-threaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top