一句话:用 LLM 思维链分链翻译 Java 并发程序为 ArkTS,并以 ThreadBridge 库在 ArkTS 上模拟 Java 共享内存模型。
HarmonyOS NEXT 要求开发者将 Android 应用迁移到以 ArkTS(TypeScript 超集)编写的 Harmony 生态,而 Java 采用共享内存实现并发、ArkTS 则基于消息传递(Actor 模型),跨语言翻译并发程序尤为困难。本文提出一个基于大语言模型的并发 Java 程序到 ArkTS 转换器:利用 LLM 进行高效代码翻译,并借助 ArkTS 的 SharedArrayBuffer API 实现 ThreadBridge 库,以复现 Java 的共享内存模型;同时运用 LLM 的思维链机制,将翻译过程分解为 TS 链、并发链和同步链,分别处理 TypeScript 语法、并发模式与同步逻辑。评估在 53 个测试样例上进行,66% 的样例成功编译,编译结果准确率为 69%。该工作为弥合 Java 与 ArkTS 的并发模型差异、减少人工重写提供了可行途径。
HarmonyOS NEXT is a distributed operating system developed to support HarmonyOS native apps. To support the new and independent Harmony ecosystem, developers are required to migrate their applications from Android to HarmonyOS. However, HarmonyOS utilizes ArkTS, a superset of TypeScript, as the programming language for application development. Hence, migrating applications to HarmonyOS requires translating programs across different program languages, e.g., Java, which is known to be very challenging, especially for concurrency programs. Java utilizes shared memory to implement concurrency programs, while ArkTS relies on message passing (i.e., Actor model). This paper presents an LLM-based concurrent Java program to ArkTS converter.Our converter utilizes large language models (LLMs) for efficient code translation, integrating ArkTS’s SharedArrayBuffer API to create ThreadBridge, a library that replicates Java’s shared memory model. Using LLM’s Chain-of-Thought mechanism, the translation process is divided into specialized chains: the TS chain, concurrency chain, and synchronization chain, each handling TypeScript syntax, concurrency patterns, and synchronization logic with precision.This study offers solutions to bridge concurrency model differences between Java and ArkTS, reducing manual code rewriting and speeding up adaptation for HarmonyOS NEXT. Experiments show the converter successfully compiles 66% of 53 test samples, with 69% accuracy for compiled results. Overall, the approach shows promise in converting concurrent Java programs to ArkTS, laying the foundation for future improvements.