Skip to content

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

Nikos Siatras edited this page Oct 9, 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 thread synchronization issues, with the goal of better understanding wait() and notify() methods.

wait() and notify() Methods

The wait() and notify() are methods of the KObject class. They were introduced to part ways with polling, which is the process of repeatedly checking for a condition to be fulfilled. Polling wastes CPU resources considerably, hence it is not preferred.

wait() Method

wait() method is a part of KObject class. When wait() method is called, the calling thread stops its execution until notify() method is invoked by some other Thread.

notify() Method

The notify() method is defined in the KObject class. It’s used to wake up any thread that’s waiting for an object, and these threads then begins execution.

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 KObject.wait() and KObject.notify() methods. These methods keep a check for a particular condition before resuming the execution.

With that in mind, we'll make use of the following:

graph TD;
    Thread1(Thread #1) --> KObject.wait(<b>KObject.wait</b><br><i>Thread pauses until an <br>other thread calls</i> <b>KObject.notify</b>);
    KObject.wait --> Continue(Thread continues...);
    
    Thread2(Thread #2)--> KObject.notify;
Loading

Thread Synchronization Examples

Simple Thread Synchronization Example

The following example synchronizes 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 achieved 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()

Multiple Threads Synchronization Examples

The following example has 2 threads. Thread #2 is waiting for a notification from thread #1 to run.

#include once "kiwi\kiwi.bi"

Dim Shared fLockObject as KObject

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

Sub Thread1_Process.run()
    while(true)
	print ""
	print Thread.currentThread().getName() & " is waiting..."
        fLockObject.wait()
        print Thread.currentThread().getName() & " is running!"
    wend
End Sub

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

Sub Thread2_Process.run()
    while(true)
        Thread.pause(2000)
        fLockObject.notify()
    wend
End Sub

print "Start !"

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

Dim runnable2 as Thread2_Process
Dim thread2 as Thread = Thread(runnable2,"Thread 2")
thread2.start() ' Start Thread2

' Wait for user input to exit
sleep()

thread1.interrupt()
thread2.interrupt()