# Automatic Refactoring Approach for Asynchronous Mechanisms with CompletableFuture
## 4 Empirical Research  
We conducted empirical research on asynchronous mechanisms to understand the current state of practice of asynchronous constructs like CompletableFuture in actual Java programs. This helps us to assess the potential opportunities for refactoring to CompletableFuture. On the other hand, we want to explore whether the CompletableFuture construct helps programmers write less complex asynchronous task code. Therefore, we addressed the following two research questions, introducing the corpus and methods first and then presenting the results for each question. Figure 3: CompletableFuture asynchronous remote acquisition and decryption of images. Figure 2: Traditional asynchronous remote acquisition and decryption of images. RQ1: How are the commonly used asynchronous executor APIs currently utilized in Java? Through this research question, we aim to understand the usage of traditional asynchronous constructs and CompletableFuture constructs to assess the potential for our refactoring work. The literature [4] has documented the use of asynchronous executor-related constructs; however, the dataset used was compiled in 2015 and may not accurately reflect the current state of open-source software implementations, particularly the use of the CompletableFuture construct introduced with Java in 2014. Using the same method as in the literature, we added 4 constructs to the 14 types of asynchronous executor constructs they analyzed. As shown in Table 2, we surveyed the use of 18 different asynchronous APIs. Among these, Executors serve as thread pool factory classes provided by JUC, FutureTask is a basic implementation class of the Future interface, the @Async annotation facilitates easy asynchronous method execution within the Spring framework, ListenableFuture and AsyncResult support callback-based asynchronous patterns, Publisher and Observable represent the reactive programming paradigm facilitating asynchronous data flow processing, SwingWorker and Task handle time-consuming tasks in Swing and JavaFX, respectively, without blocking the UI thread, AsyncResponse is used for asynchronous response handling in web services, and ManagedTask, Vert.x Future, and Akka Futures provide solutions for asynchronous operations in enterprise-level concurrent programming and event-driven architectures. Corpus-1. Similar to the literature [4], we utilized BOA [23] to analyze open-source Java projects. BOA is a language and infrastructure designed for mining software repositories. We used its latest Java dataset (\"2022 Jan/Java\" in BOA), which includes 91,403 projects on GitHub with Java as the primary language (the highest proportion of Java code) and has been converted into AST (Abstract Syntax Tree). Method. Using the BOA language, we accessed the AST of projects in the dataset through the visitor pattern to determine if the Import section contains any of the asynchronous APIs listed in Table 2. For Threads, since they belong to the java.lang package and do not require an import to be used, we traversed all Type nodes within the AST. Result. Table 2 presents the usage statistics of the asynchronous APIs. Out of the 91,403 projects, 34,492 projects contained asynchronous APIs, accounting for 37.74% of all projects. The most frequently used API was Thread, accounting for 30.07% of the projects.