如何将类Java语言的线程模型（内存共享）的实现方式转换成在ArkTS的线程模型下（内存隔离）的实现方式
====================================================

可以利用TaskPool接口转换，具体分为以下四个场景：

*   场景一：主线程将独立任务放到子线程执行。代码示例：
    
    共享内存写法：
    
    class Task {
      static run(args) {
       _ // Do some independent tasks_
      }
    }
    let thread = new Thread(() => {
      let result = Task.run(args)
     _ // deal with result_
    })
    
    ArkTS写法：
    
    import { taskpool } from '@kit.ArkTS';
    
    @Concurrent
    function run(args: number) {
     _ // Do some independent tasks_
    }
    let task: taskpool.Task = new taskpool.Task(run, 100); // 100: test number
    taskpool.execute(task).then((res) => {
     _ // Return result_
    });
    
*   场景二：主线程在子线程使用类对象实例。代码示例：
    
    共享内存写法：
    
    class Material {
      action(args) {
      _  // Do some independent tasks_
      }
    }
    let material = new Material()
    let thread = new Thread(() => {
      let result = material.action(args)
      _// deal with result_
    })
    
    ArkTS写法：
    
    import { taskpool } from '@kit.ArkTS';
    
    @Concurrent
    function runner(material: Material): void {
      return material.action(100); _// 100: test number_
    }
    @Sendable
    class Material {
      action(args: number) {
       _ // Do some independent tasks_
      }
    }
    let material = new Material()
    taskpool.execute(runner, material).then((ret) => {
      _// Return result_
    })
    
*   场景三：子线程更新主线程状态。代码示例：
    
    共享内存写法：
    
    class Task {
        run(args) {
           _ // deal with result_
            runOnUiThread(() => {
                UpdateUI(result)
            })
        }
    }
    let task = new Task()
    let thread = new Thread(() => {
        let result = task.run(args)
      _  // Processing results_
    })
    
    ArkTS写法：
    
    import taskpool from '@ohos.taskpool'
    
    _// let result: Object\[\] | undefined = undefined_
    
    @Concurrent
    function runner(task:Task) {
      task.run()
    }
    @Sendable
    class Task {
      run(args?: Object\[\] | undefined) {
       _ // Do some independent tasks_
        taskpool.Task.sendData(JsResult)
      }
    }
    let task = new Task()
    let run = new taskpool.Task(runner, task)
    run.onReceiveData((result?: Function | undefined) => {
      UpdateUI(result)
    })
    taskpool.execute(run).then((ret) => {
     _ // Return result_
    })
    
*   场景四：子线程同步调用主线程接口。代码示例：
    
    class SdkU3d {
        static getInst() {
            return SdkMgr.getInst();
        }
        getPropStr(str: string) {
            return xx;
        }
    }
    let thread = new Thread(() => {
      _  // In the game thread_
        let sdk = SdkU3d.getInst()
        let ret = sdk.getPropStr("xx")
    })
    
    ArkTS写法：
    
    import { MessageEvents, taskpool, worker } from '@kit.ArkTS';
    class SdkU3d {
      static getInst(): Object {
        return SdkMgr.getInst();
      }
      getPropStr(str: string) { }
    }
    let workerInstance = new worker.ThreadWorker("xx/worker.ts");
    workerInstance.registerGlobalCallObject("instance\_xx", SdkU3d.getInst());
    workerInstance.postMessage("start");
    _// In the game worker thread_
    const mainPort = worker.workerPort;
    mainPort.onmessage = (e: MessageEvents): void => {
      let ret = mainPort.callGlobalCallObjectMethod("instance\_xx", "getPropStr", 100); // 100: test number
    }
    

**参考链接**

[并发概述](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/concurrency-overview)