Using Threads and Synchronisation


Using Threads in Jabaco is quite simple. Create a Class and implement the "java#lang#Runnable"-Interface. Create a new Thread-Instance ("java#lang#Thread") and refer your "Runnable"-Class in the Thread-Class Constructor. This Interface implements a "Public Sub run()"-Method, which will be invoked by the Thread-Instance when you call the "start"-Method of your Thread-Class. If you have never heared about Threads that may sounds complicated, but don't worry - have a look to the sample.

For more Informations about Thread-Handling in Java (that is of course similar to Jabaco) have a look to:

I'll show you a sample and demonstrate how you can synchronize Threads. We create a Thread that works with a variable and a Thread that observe the same variable. Both threads access the same resource and that could be unwanted. I'll show you how to solve this problem.

At first we create our Threads in the Main-Module:

' Module: Main.jsrc
Dim myWorkerClass As New WorkerClass() ' Create the Worker-Class (see below)
Dim myWorkerThread As New Thread(myWorkerClass) ' Use the Worker-Class with the Worker-Thread
Dim myTestThread As New Thread(New TestClass(myWorkerClass))
 
Public Sub main(ByJava args() As String)
   Call myWorkerThread.start() ' This Thread will change our shared resource in a loop
   Call myTestThread.start()  ' ... and this will observe our shared resource
End Sub

Our worker will change our shared resource in a loop.

' CLASS: WorkerClass.jsrc
Implements java#lang#Runnable  ' This Interface is necessary for usage with a Thread-Class
Dim myItem As Long ' The shared resource that we will change and observe

Public Property Get Item() As Long ' Public access for the shared resource
Item = myItem
End Property
 
Public Sub run() 
' This method is implemented by "Runnable" and will be invoked inside the JavaThread
Do While True
Call RefreshItem()
myCounter = myCounter + 1
Loop
End Sub
 
Private Sub RefreshItem() 
   myItem = 0 ' Set var to 0
   myItem = 1 ' Set var to 1
   myItem = 23 ' Our observer should get 23 from the public item-property
End Sub

Now we implement an observer.

' CLASS: TestClass.jsrc
Implements java#lang#Runnable
Dim myOwner As WorkerClass ' Our worker with shared resource

Public Sub TestClass(refOwner As WorkerClass) ' The TestClass-Constructor
   myOwner = refOwner
End Sub
 
Public Sub run()
   Do While True
  Call TestItem() ' Check the resource
Loop
End Sub
 
Public Sub TestItem() 
   ' The Item is produced by WorkerClass.RefreshItem(). It should be 23.
   If myOwner.Item <> 23 Then
      ' If it isn't 23 we would like to know what it is
      out.println "Item: " & myOwner.Item   
   End If
End Sub


The result of our test is something like that:

  • Item: 0
  • Item: 1
  • Item: 0
  • Item: 1
  • Item: 1

Explication: The Observer/Test-Class asks for the Item while it is used inside the WorkerClass.RefreshItem(). Prevent this by synchronize the access to the Worker-Class.

Simply change the attribute of the methods from:

Public Property Get Item() As Long
Private Sub RefreshItem()

to:

Public Synchronized Property Get Item() As Long
Private Synchronized Sub RefreshItem()

By invoke a "Synchronized"-method the owner-class of this method will be locked for other threads. The other threads will wait for the end of execution. So we could synchronize the access to our resource.

Retrieved from "http://www.jabaco.org/wiki/Using_Threads_and_Synchronisation"

This page has been accessed 5,657 times. This page was last modified on 15 December 2008, at 15:09.



This page has been accessed 5,657 times. This page was last modified on 15 December 2008, at 15:09.