# Refactoring for Java-Structured Concurrency
## 1 Introduction  
Structured concurrency is a concurrent programming method that constrains the concurrent behavior between tasks by defining a concurrent region, enhancing the program's maintainability and observability. The structured concurrency framework was first introduced in JDK19 [1], which defines a clear scope for a set of tasks that are executed simultaneously to manage them, aiming at more precise control and management of concurrent tasks. Structured concurrency can help to simplify thread management issues in concurrent programming and can realize the programming style of one task per thread. In recent years, various methods and tools have been proposed in both academia and industry to enhance the performance of concurrent programs. Some researchers have proposed methods for converting sequential code in Java to concurrent code. [PERSON] et al. [2] proposed a method to improve the responsiveness of mobile wearable devices by converting long-running blocking code to asynchronous execution. [PERSON] et al. [3] proposed how to utilize program analysis techniques to parallelize identified serial code. Other researchers have suggested using new features in the JDK to transform serial streams to parallel streams. [PERSON] et al. [4] proposed a refactoring method to transform different _for_ loops to the corresponding stream structures to improve the execution efficiency of the loops. [PERSON] et al. [5] proposed a refactoring method called _Relooper_, which transformsserial code arrays to parallel arrays that can be executed in parallel. Additionally, there has been work on lock-related refactoring in parallel programs; for example, [PERSON] et al. [6] proposed an automatic refactoring tool called _Relocker_, which helps programmers to refactor synchronized blocks to reentrant or read-write locks. [PERSON] et al. introduced refactoring tools _FineLock_[7] and _Clock_[8] to improve lock patterns. Most of the refactoring tools mentioned above are built based on the Java 8 APIs, such as Lambda expressions and the Stream API. These refactoring tools, although they have played certain roles in their specific application scenarios, because of the limitations of their technical foundation, none of them can effectively solve the complexity problems brought about by various aspects such as task coordination, error propagation control, and complex concurrent logic organization, in concurrent programming. Therefore, it is difficult for them to achieve in-depth optimization and efficient management of concurrent programming. However, our tool is based on the structured concurrency features in JDK 19. It can provide developers with a convenient, safe, and efficient concurrent programming model. In Java, although unstructured concurrency can be refactored to structured concurrency manually, manual refactoring is time consuming, labor intensive, and prone to errors. Automated refactoring can be employed, but it mainly faces the following challenges: (1) the identification of unstructured concurrency and refactoring it to structured concurrency through program analysis, (2) ensuring the independence of tasks during structured concurrency refactoring, and (3) ensuring that tasks do not exceed the boundaries of the structured-concurrency control during the structured-concurrency process. To address these issues, this paper proposes an automated refactoring method. This method first uses visitor pattern analysis to locate the unstructured code for refactoring. Then, it performs a precondition to filter the unstructured code and conducts a scope analysis. Scope analysis ensures that the variable scope remains unchanged after refactoring. Finally, the refactoring is completed in the AST (abstract syntax tree) based on the results obtained. Using this method, we implemented the automated refactoring tool _ReStruct_ as a plugin within the Eclipse JDT [9] framework. This tool was used to conduct experiments on seven real-world projects, including SystemML [10] and Light-4j [11].