Threads and shared data problem

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Dec 2008
Posts: 2
Reputation: midnightray is an unknown quantity at this point 
Solved Threads: 0
midnightray midnightray is offline Offline
Newbie Poster

Threads and shared data problem

 
0
  #1
Dec 11th, 2008
Hello,

Firstly your forum has solved many of my questions in the past, I have had to register to find the solution to this one.

The current application I am working on is multi-threaded and I am really getting confused with VB.NET's way of doing things (ex Java programmer).

Basically,

A schedule class runs every 60 seconds querying our database for new "Job's" to do. If it finds "Job's" it spawns a Job thread for each job and waits another 60 seconds.

A Job class fetches required "Report's" of the "Job" and spawns a "Report" thread for each of the "Reports". (Note: a report thread can take 1 sec to 20 sec to run)

My problem is, I have implemented a delegate sub in the "Job" class that the "Report" class threads use. Leading to my problem that when two "Job" threads are running the "Report" threads aren't talking back to the correct "Job" thread. The "Report" threads need to pass back the processed data to the "Job" thread for merging.

Are delegates the best way to handle communication between my threads. The examples I have seen the Delegate subs or functions have been in the same class. My problem is that I have had to make it Shared in the Job class so the Report class can see it without having to make an instance of the Job class.

(I would post code, but its a lot of code. Will post snippets soon once I can make it simpler to understand)

Summary:
Schedule (Polls database for Jobs)
Job (Handles the Job from the database)
Report (Handles the Report part of the Job)
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 2
Reputation: midnightray is an unknown quantity at this point 
Solved Threads: 0
midnightray midnightray is offline Offline
Newbie Poster

Re: Threads and shared data problem

 
0
  #2
Dec 11th, 2008
(Sorry for the double post, the edit button has gone)

Here is the code, the problem is illustrated when ran, the Job class expects 3 returns but the "Report" thread return to both classes (6 rather than 3).

I need a way for the report threads to only report to their job thread and not any others running!
(Used to run it as a console application)
  1. Module Module1
  2.  
  3. Sub Main()
  4. Console.WriteLine("(Module) Running schedule prototype")
  5. Dim schedule As New ScheduleClass()
  6. schedule.run()
  7. End Sub
  8.  
  9. End Module

The schedule class
  1. Imports System.Threading
  2.  
  3. Public Class ScheduleClass
  4.  
  5. Public Sub run()
  6.  
  7. Try
  8. ' Thread name counter
  9. Dim thread_counter As Integer = 0
  10.  
  11. While True
  12.  
  13. ' Simulate 2 jobs at once
  14. newJobThread("Job Thread " & thread_counter.ToString)
  15. newJobThread("Job Thread " & thread_counter.ToString + 1)
  16. thread_counter = thread_counter + 1
  17. Thread.Sleep(60000)
  18.  
  19. End While
  20.  
  21. Catch ex As Exception
  22.  
  23. End Try
  24.  
  25. End Sub
  26.  
  27. Private Sub newJobThread(ByVal thread_name As String)
  28. Dim job As New JobClass(thread_name)
  29. Dim thread As New Thread(AddressOf job.run)
  30. thread.Name = thread_name
  31. thread.Priority = ThreadPriority.Normal
  32. thread.Start()
  33. End Sub
  34. End Class

The Job class
  1. Imports System.Threading
  2.  
  3. Public Class JobClass
  4.  
  5. Dim thread_name As String
  6. Shared job_data As ArrayList
  7.  
  8. Public Sub New(ByVal thread_name As String)
  9. Me.thread_name = thread_name
  10. job_data = New ArrayList(3)
  11. Console.WriteLine("(JobClass) New thread with name: " & Me.thread_name)
  12. End Sub
  13.  
  14. Public Sub run()
  15.  
  16. ' Simulate generating 3 reports for the job thread
  17. newReportThread(Me.thread_name & ": Report Thread 1")
  18. newReportThread(Me.thread_name & ": Report Thread 2")
  19. newReportThread(Me.thread_name & ": Report Thread 3")
  20.  
  21. While job_data.Count <> 3
  22. Console.WriteLine("(JobClass) Not all data returned (Only " & job_data.Count & " out of 3)")
  23. Thread.Sleep(3000)
  24. End While
  25.  
  26. Console.WriteLine("(JobClass) All data returned now outputing for thread: " & Me.thread_name)
  27.  
  28. Dim loop_counter As Integer = 0
  29.  
  30. While loop_counter < job_data.Count
  31. Console.WriteLine("(JobClass) Item(" & loop_counter & ") = " & job_data.Item(loop_counter).ToString)
  32. loop_counter = loop_counter + 1
  33. End While
  34.  
  35. End Sub
  36.  
  37. Private Sub newReportThread(ByVal thread_name As String)
  38. Dim report As New ReportClass(Me.thread_name & ":" & thread_name)
  39. Dim thread As New Thread(AddressOf report.run)
  40. thread.Name = Me.thread_name & ":" & thread_name
  41. thread.Priority = ThreadPriority.Normal
  42.  
  43. thread.Start()
  44. End Sub
  45.  
  46. Public Shared Sub returnReportThreadData(ByVal data As String)
  47.  
  48. SyncLock (job_data)
  49. job_data.Add(data)
  50. End SyncLock
  51.  
  52. End Sub
  53. End Class

The Report class
  1. Imports System.Threading
  2.  
  3. Public Class ReportClass
  4.  
  5. Dim thread_name As String
  6.  
  7. Public Sub New(ByVal thread_name As String)
  8. Me.thread_name = thread_name
  9. Console.WriteLine("(ReportClass) New Report thread: " & Me.thread_name)
  10. End Sub
  11.  
  12. Public Sub run()
  13. Dim handler As New returnData(AddressOf JobClass.returnReportThreadData)
  14.  
  15. Dim rand As New Random
  16.  
  17. ' Simulate waiting for the report to be completed
  18. Thread.Sleep(1000 * rand.Next(1, 5))
  19.  
  20. handler.Invoke("Hi from thread: " & Me.thread_name)
  21.  
  22. End Sub
  23.  
  24. Delegate Sub returnData(ByVal data As String)
  25. End Class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC