You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

Manuel

Administrator

  • "Manuel" is male
  • "Manuel" started this thread

Posts: 256

Date of registration: Jul 16th 2008

Location: Erlangen, Germany

Occupation: Software Developer

Hobbies: Jabaco, game theory, text-mining

  • Send private message

1

Thursday, August 21st 2008, 10:41pm

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: http://java.sun.com/j2se/1.4.2/docs/api/…ang/Thread.html

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:

Jabaco Source

1
2
3
4
5
6
7
8
9
' 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.

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
' 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.

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
' 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() 
   If myOwner.Item <> 23 Then ' The Item is produced by WorkerClass.RefreshItem(). It should be 23.
  out.println "Item: " & myOwner.Item   ' If it isn't 23 we would like to know what it is
   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.

Rate this thread
WoltLab Burning Board