Skip to content

kiwi.lang.Thread wait() and notify() Methods in Kiwi

Nikos Siatras edited this page Oct 4, 2022 · 26 revisions

Overview

In this tutorial, we'll look at one of the most fundamental mechanisms in Kiwi — thread synchronization. We'll first discuss some essential concurrency-related terms and methodologies. And we'll develop a simple application where we'll deal with concurrency issues, with the goal of better understanding wait() and notify().

Thread Synchronization in Kiwi

In a multithreaded environment, multiple threads might try to modify the same resource. Not managing threads properly will of course lead to consistency issues. One tool we can use to coordinate actions of multiple threads in Kiwi is guarded blocks. Such blocks keep a check for a particular condition before resuming the execution. With that in mind, we'll make use of the following:

Thread Synchronization Example

The following example synchronizes 2 threads, the program's main thread and "My Thread". The program's main thread will wait for "My Thread" to finish. This "wait until method" is achived using the wait() and notify() methods of the fWaitForThreadToFinish KObject.

#include once "kiwi\kiwi.bi"

Dim Shared fWaitForThreadToFinish as KObject

Type Thread1_Process extends Runnable 
    public:
        Declare Sub run()
End Type

Sub Thread1_Process.run()
	for i as Integer = 1 to 10
        print Thread.currentThread().getName() & " " & i
        Thread.pause(1000)
    next
    
    ' Notify fWaitForThreadToFinish
    fWaitForThreadToFinish.notify()
End Sub



Dim runnable1 as Thread1_Process
Dim thread1 as Thread = Thread(runnable1, "My Thread")
thread1.start() ' Start Thread1

' Wait for fWaitForThreadToFinish to get notified
fWaitForThreadToFinish.wait()

Home

Kiwi.DB

  • MySQL/MariaDB - Coming to v1.0.2

Kiwi.IO

Kiwi.lang

Kiwi.locale

Kiwi.time

Kiwi.util

Clone this wiki locally