Skip to content

kiwi.lang.Thread

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

A Thread is a thread of execution in a program. Kiwi allows an application to have multiple threads of execution running concurrently on all platforms except DOS.

Thread.start()

Sub Thread.start()

Causes this thread to begin execution

Thread.interrupt()

Sub Thread.start()

Interrupts this thread.
If this thread is blocked in an invocation of the Thread.Pause, KObject.wait() or KObject.wait(LongInt) methods then this method will resume the thread.

Thread.run()

Sub Thread.run()

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called otherwise, this method does nothing and returns.

Thread.getName()

function Thread.getName() as String

Return the Thread's name

Thread.setName()

sub Thread.setName(newName as String) 

Changes the name of this thread to be equal to the argument "newName".
@param newName is the new name for this thread.

Thread.isAlive()

function Thread.isAlive() as Boolean

Returns true if this thread is alive otherwise false. A thread is alive if it has been started and has not yet died.
@return true if this thread is alive otherwise false.

Thread.getThreadLock()

function Thread.getThreadLock() as Any PTR

Returns the ThreadLock of this thread. This method is mainly used from Thread.sleep method

Thread.currentThread()

static function Thread.currentThread() byref as Thread

Returns the current thread instance

Example:

' This will print the name of the current thread.
print Thread.currentThread().getName()

Thread.pause()

static sub Thread.pause(ms as Long)

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

Thread Examples

Example - Simple Thread Start

The following example code starts a thread and keeps the program running until the thread finishes.

Keep in mind: In order to start a Thread using Kiwi we need to declare a Runnable object. The Runnable object should be implemented by any Type whose instances are intended to be executed by a thread. The type must define a method of no arguments called run(). After we declare the Runnable object (runnable1 for our example) we pass a new instance of it to the thread. To start the thread we simply call the Thread.start() method.

#include once "kiwi\kiwi.bi"

' Declare a new Runnable Object. Every thread needs a Runnable
Type Thread1_Process extends Runnable 
	Declare Sub run()
End Type

Sub Thread1_Process.run()
	for i as Integer = 0 to 9
		print i
		Thread.pause(1000)
	next
	print "Thread 1 Finished" ' This prints after fKeepRunning turns to false
End Sub

' Initialize a new runnable object and pass it to a new thread
Dim runnable1 as Thread1_Process
Dim thread1 as Thread = Thread(runnable1)
thread1.start() ' Start the thread

while(thread1.isAlive() = false)
	' Wait for thread to start
wend

while(thread1.isAlive() = true)
	' Wait for thread to finish
wend

Example - Simple Thread Showing Time

The following example code creates a new Thread that prints Date and Time every 5000ms. The program waits for the user to press any key in order to terminate the thread.

#include once "kiwi\kiwi.bi"
#include once "kiwi\time.bi"

Dim Shared fKeepRunning as Boolean = true
Dim Shared fWaitForThreadToStart as KObject
Dim Shared fWaitForThreadToFinish as KObject
Dim tmpStr as String

print "This program displays time every 5seconds"
print "Press Enter to interrupt the thread and exit"
print ""

' Declare a new Runnable Object. A threads needs a Runnable
' in order to start
Type Thread1_Process extends Runnable 
	Declare Sub run()
End Type

Sub Thread1_Process.run()

	fWaitForThreadToStart.notify() ' Notify fWaitForThreadToStart
	
	Dim cal as Calendar = Calendar()
	while (fKeepRunning)
		Dim dtNow as DateTime = cal.getTime()
		print "Time at thread " & Thread.currentThread().getName() & " is " & dtNow.toString()
		Thread.pause(5000)
	wend
	print "Thread 1 Finished" ' This prints after fKeepRunning turns to false
	
	fWaitForThreadToFinish.notify() ' Notify fWaitForThreadToFinish
	 
End Sub

' Initialize a new runnable object and pass it to a new thread
Dim runnable1 as Thread1_Process
Dim thread1 as Thread = Thread(runnable1, "Thread #1")
thread1.start() ' Start the thread

' Wait for thread to start
fWaitForThreadToStart.wait()
print "Thread 1 is live: " & thread1.isAlive()

' Wait for user input in order to terminate the program.
' To terminate the program set fKeepRunning = false
input "", tmpStr
fKeepRunning = false

' Wait for thread 1 to exit
print "Waiting for thread to finish..."
thread1.interrupt() ' Interrupt the thread
fWaitForThreadToFinish.wait()