Skip to main content

Class: Thread

threading/thread.Thread

Constructors

constructor

โ€ข new Thread(id)

Creates a Thread instance, sets up it's worker and starts it's cleaning loop.

Parameters

NameTypeDescription
idstringA string to be used as an identifier for this thread.

Defined in

src/threading/thread.ts:30

Properties

cleanRate

โ€ข Private cleanRate: number = 100

Defined in

src/threading/thread.ts:23


currentTask

โ€ข Private currentTask: ThreadTask

Defined in

src/threading/thread.ts:19


id

โ€ข Private id: string

Defined in

src/threading/thread.ts:22


inUse

โ€ข Private inUse: boolean = false

Defined in

src/threading/thread.ts:18


maxQueueSize

โ€ข Private maxQueueSize: number = Infinity

Defined in

src/threading/thread.ts:21


queue

โ€ข Private queue: ThreadTask[] = []

Defined in

src/threading/thread.ts:20


worker

โ€ข Private worker: BLZWorker

Defined in

src/threading/thread.ts:17

Methods

addTask

โ–ธ addTask(task, skipQueue?): boolean

Attempts to run a task on the thread or add it to the queue.

Parameters

NameTypeDefault valueDescription
taskThreadTaskundefinedThe task to run or queue.
skipQueuebooleanfalseIf true then the max queue size is ignored and the task is prepended to the front of the queue

Returns

boolean

True when the task is ran or queued, false otherwise

Defined in

src/threading/thread.ts:71


clean

โ–ธ Private clean(): void

Attempts to clean out the thread's queue whenever the thread has hung.

A hung thread is one in which the thread is not in use but it has task(s) in it's queue.

Returns

void

Defined in

src/threading/thread.ts:130


getId

โ–ธ getId(): string

Gets the thread's id.

Returns

string

The thread's id

Defined in

src/threading/thread.ts:172


getInUse

โ–ธ getInUse(): boolean

Returns wether the thread is currently in use.

This is true when the thread's worker is currently processing a task.

Returns

boolean

If the thread is currently in use.

Defined in

src/threading/thread.ts:145


getNumInQueue

โ–ธ getNumInQueue(): number

Gets the number of task in the thread's queue.

Returns

number

The length of the thread's queue.

Defined in

src/threading/thread.ts:163


handleMessage

โ–ธ Private handleMessage(msg): void

Handles a message from the thread's worker

Parameters

NameTypeDescription
msgWorkerMessageThe message received from the worker

Returns

void

Defined in

src/threading/thread.ts:52


isFree

โ–ธ isFree(): boolean

A thread is free when it is not in use or the queue size is below the thread's maximum queue size

Returns

boolean

true/false depending on if the thread is free

Defined in

src/threading/thread.ts:154


log

โ–ธ Private log(...params): void

Prepends a console.log call with the thread's id.

Parameters

NameTypeDescription
...paramsanyParams for console.log

Returns

void

Defined in

src/threading/thread.ts:181


nextTask

โ–ธ Private nextTask(): void

Sends the next task in the thread's queue to it's worker.

If the queue is empty then the thread marks itself as not in use.

Returns

void

Defined in

src/threading/thread.ts:116


sendTask

โ–ธ Private sendTask(task): void

Sends a task to the thread's worker, saves a reference to the sent task and marks the thread as in use.

No checks are made to see if the thread is in use before sending the task.

Parameters

NameTypeDescription
taskThreadTaskThe task to send to the thread's worker.

Returns

void

Defined in

src/threading/thread.ts:105


setupWorker

โ–ธ Private setupWorker(): void

Sets up the thread's worker and it's associated events.

Returns

void

Defined in

src/threading/thread.ts:40