-
Notifications
You must be signed in to change notification settings - Fork 3
kiwi.lang.Thread wait() and notify() Methods in Kiwi
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().
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:
- KObject.wait() to suspend a thread
- KObject.notify() to wake a thread up
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()
- MySQL/MariaDB - Coming to v1.0.2
- ArrayList
- Comparator
- HashMap - Coming to v1.0.2
- Queue