资源加载中... loading...

Thread

Thread对象可以由threading.Thread()threading.getThread()threading.mainThread()threading.currentThread()创建或者返回。

peekMessage

peekMessage()函数用于从线程获取消息。

peekMessage()函数返回当前线程对象关联的线程收到的消息。

string、number、bool、object、array、空值等系统支持的所有类型

peekMessage() peekMessage(timeout)

参数timeout为超时设置,会按照该参数设置的毫秒数阻塞等待,返回数据;没有数据并且超时则返回空值。如果timeout设置为0或者不传timeout参数,则表示一直阻塞等待,直到接收到通道中的数据。如果timeout设置为-1,则表示不阻塞并且立即返回数据,没有数据时返回空值。

timeout false number

function main() {
    var t1 = threading.Thread(function() {
        for (var i = 0; i < 10; i++) {
            Log("thread1 postMessage():", i)
            threading.mainThread().postMessage(i)
            Sleep(500)
        }        
    })

    while (true) {
        var msg = threading.currentThread().peekMessage()
        Log("main peekMessage():", msg)
        if (msg == 9) {
            break
        }
        Sleep(1000)
    }

    t1.join()
}

并发线程中向主线程发送消息。

在编写程序时需要注意线程死锁问题。

{@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

postMessage

postMessage()函数用于向线程发送消息。

postMessage(msg)

参数msg为所要发送的消息。

msg true string、number、bool、object、array、空值等系统支持的任意类型

function main() {
    var t1 = threading.Thread(function() {
        for (var i = 0; i < 10; i++) {
            Log("thread1 postMessage():", i)
            threading.mainThread().postMessage(i)
            Sleep(500)
        }        
    })
    for (var i = 0; i < 10; i++) {
        var event = threading.mainThread().eventLoop()
        Log("main event:", event)
        Sleep(500)
    }
    t1.join()
}

在并发的线程中发送消息,使用eventLoop()接收消息通知。

当一个线程的执行函数中调用postMessage()函数发出信号、数据时,也会产生消息事件。 可以用eventLoop()函数收到消息通知。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

join

join()函数用于等待线程退出,并回收系统资源。

ThreadRet对象包含执行结果的相关数据,包含的属性如下:

  • id: 线程Id。
  • terminated: 线程是否被强制结束。
  • elapsed: 线程的运行时间(纳秒)。
  • ret: 线程函数的返回值。

ThreadRet对象

join() join(timeout)

timeout参数用于设置等待线程结束的超时时间,单位为毫秒。timeout参数设置为0或者不设置timeout参数时join()函数会阻塞,等待到线程执行结束。timeout参数设置为-1时,join()函数会立即返回。

timeout false number

function main() {
    var t1 = threading.Thread(function() {
        Log("Hello thread1")
        Sleep(5000)
    })

    var ret = t1.join(1000)
    Log("ret:", ret)   // ret: undefined

    ret = t1.join()
    Log("ret:", ret)   // ret: {"id":1,"terminated":false,"elapsed":5003252000}
}

测试join()函数超时,输出返回值。

join()函数超时,返回undefined

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

terminate

terminate()函数用于强制结束线程,释放创建线程使用的硬件资源。

terminate()

function main() {
    var t1 = threading.Thread(function() {
        for (var i = 0; i < 10; i++) {
            Log("thread1 i:", i)
            Sleep(1000)
        }
    })

    Sleep(3000)
    t1.terminate()
    Log("after t1.terminate()")

    while (true) {
        LogStatus(_D())
        Sleep(1000)
    }
}

强制终止一个线程的执行,在强制终止一个线程之后,日志中不再有这个线程输出的内容。

对于terminate()函数强制结束的线程,无法再使用join()函数等待结束。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

getData

getData()函数用于访问线程环境中记录的变量。数据在线程没有被执行join()函数(等待退出成功)并且没有被执行terminate()函数(强制终止线程)的情况下有效。

getData()函数返回当前线程环境中储存的键-值对中key参数对应的键值。

string、number、bool、object、array、空值等系统支持的所有类型

getData() getData(key)

key参数为储存的键-值对的键名。

key true string

function main() {
    var t1 = threading.Thread(function() {
        for (var i = 0; i < 5; i++) {
            threading.currentThread().setData("count", i)
            Log(`setData("count"):`, i)
            Sleep(1000)
        }
    })
    for (var i = 0; i < 5; i++) {
        var count = threading.getThread(t1.id()).getData("count")
        Log(`getData("count"):`, count)
        Sleep(1000)
    }
    t1.join()
}

在并发线程的环境中记录键名为count的值,然后在主线程中读取count的键值。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

setData

setData()函数用于在线程环境储存变量。

setData(key, value)

key参数用于指定储存的键-值对的键名。

key true string value参数用于指定储存的键-值对的键值。

value true string、number、bool、object、array、空值等系统支持的任意类型

function main() {
    var t1 = threading.Thread(function() {
        threading.currentThread().setData("data", 100)
    })
    Sleep(1000)
    Log(`t1.getData("data"):`, t1.getData("data"))
    t1.join()
}

并发的线程中设置键值对,主线程中读取这个键值对。

数据在线程没有被执行join()函数(等待退出成功)并且没有被执行terminate()函数(强制终止线程)的情况下有效。参数value的值必须是可序列化的变量。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

id

id()函数用于返回当前多线程对象实例的threadId

id()函数的返回值为threadId

number

id()

function main() {
    var t1 = threading.Thread(function() {
        threading.currentThread().setData("data", 100)
    })
    Log(`t1.id():`, t1.id())
    t1.join()
}

创建一个并发运行的线程,在主线程输出这个并发线程的threadId

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}

name

name()函数用于返回当前多线程对象实例的名称。

name()函数返回值为并发的线程名称。

string

name()

function main() {
    var t1 = threading.Thread(function() {
        threading.currentThread().setData("data", 100)
    })
    Log(`t1.name():`, t1.name())  // t1.name(): Thread-1
    t1.join()
}

创建一个并发运行的线程,在主线程输出这个并发线程的名称。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/eventLoop eventLoop}

eventLoop

eventLoop()函数用于监听线程收到的事件。

eventLoop()函数返回当前线程收到的事件信息,查看事件信息结构

object、空值

eventLoop() eventLoop(timeout)

参数timeout为超时设置,单位为毫秒。参数timeout如果设置为0则等待有事件发生才返回,如果大于0就是设置事件等待超时,小于0立即返回最近事件。

timeout false number

function main() {
    var t1 = threading.Thread(function() {
        while (true) {
            var eventMsg = threading.currentThread().eventLoop()     // 阻塞等待
            // 2024-11-14 10:14:18 thread1 eventMsg: {"Seq":1,"Event":"thread","ThreadId":0,"Index":1,"Queue":0,"Nano":1731550458699947000}
            Log(_D(), "thread1 eventMsg:", eventMsg)
        }
    })

    var t2 = threading.Thread(function() {
        while (true) {
            var eventMsg = threading.currentThread().eventLoop(-1)   // 立即返回
            Log(_D(), "thread2 eventMsg:", eventMsg)
            Sleep(5000)
        }
    })

    var t3 = threading.Thread(function() {
        while (true) {
            var eventMsg = threading.currentThread().eventLoop(3000) // 设置3秒超时
            Log(_D(), "thread3 eventMsg:", eventMsg)
        }
    })

    t1.postMessage("Hello ", t1.name())
    t2.postMessage("Hello ", t2.name())
    t3.postMessage("Hello ", t3.name())
    t1.join()
    t2.join()
    t3.join()
}

并发执行3个线程,输出接收到的事件信息,超时或者立即返回时输出的是空值。

eventLoop()函数的处理机制与全局函数EventLoop()一致。

{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}

threading ThreadLock