Object built-in object

ObjectCondition

condition variable object

Condition variables are a mechanism that uses global variables shared between fibers for synchronization. It mainly includes two actions: 1) One thread waits for a certain condition to be true and suspends itself; 2) Another thread makes the condition true, And notify the waiting fiber to execute downward.

To prevent races, each condition variable needs aLockcooperation (LockYou can explicitly create it yourself and pass it in, or you can let fibjs create it for you)

By using condition variables, one condition variable can be used to control the switching of a batch of fibers;

The following are two examples of fiber scheduling:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
var coroutine = require("coroutine"); var cond = new coroutine.Condition(); var ready = false; var state = "ready"; function funcwait() { cond.acquire(); while (!ready) cond.wait(); state = "go" cond.release(); } coroutine.start(funcwait); cond.acquire(); console.log(state) ready = true; cond.notify(); coroutine.sleep(); console.log(state);

will output:

1 2
ready go

inheritance relationship

Constructor

Condition

Condition variable constructor (the lock required for the condition variable is constructed internally by fibjs)

1
new Condition();

condition variable constructor

1
new Condition(Lock lock);

Call parameters:

  • lock:Lock, using self-constructed locks

member function

wait

wait for a condition variable

1
Boolean Condition.wait(Integer timeout = -1);

Call parameters:

  • timeout: Integer, specifies the timeout in milliseconds. The default is -1, which means it will never timeout.

Return results:

  • Boolean, returns true if the acquisition is successful, returns false if it times out.

notify

Notify a blocked fiber (the last one added to the fiber pool) to continue execution downwards

1
Condition.notify();

notifyAll

Notify all blocked fibers to continue execution downwards

1
Condition.notifyAll();

acquire

Get ownership of the lock

1
Boolean Condition.acquire(Boolean blocking = true);

Call parameters:

  • blocking: Boolean, specifies whether to wait, wait when true, default is true

Return results:

  • Boolean, returns whether the lock was successfully acquired, true indicates successful acquisition.

The acquire method is used to obtain ownership of the lock. When the lock is in the acquireable state, this method returns true immediately.

When the lock cannot be acquired and blocking is true, the current fiber goes to sleep. When other fibers release the lock, this method returns true.

When the lock cannot be acquired and blocking is false, the method returns false.


release

Release ownership of the lock

1
Condition.release();

This method will release ownership of the lock or throw an error if the current fiber does not own the lock.


count

Query the current number of waiting tasks

1
Integer Condition.count();

Return results:

  • Integer, returns the number of tasks

toString

Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.

1
String Condition.toString();

Return results:

  • String, returns the string representation of the object

toJSON

Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.

1
Value Condition.toJSON(String key = "");

Call parameters:

  • key: String, not used

Return results:

  • Value, returns a value containing JSON serializable