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
| Name | Type | Description |
|---|---|---|
id | string | A 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
| Name | Type | Default value | Description |
|---|---|---|---|
task | ThreadTask | undefined | The task to run or queue. |
skipQueue | boolean | false | If 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
| Name | Type | Description |
|---|---|---|
msg | WorkerMessage | The 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
| Name | Type | Description |
|---|---|---|
...params | any | Params 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
| Name | Type | Description |
|---|---|---|
task | ThreadTask | The 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