diff a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/file/Locations.java
@@ -128,12 +128,14 @@
 
     static final Path javaHome = FileSystems.getDefault().getPath(System.getProperty("java.home"));
     static final Path thisSystemModules = javaHome.resolve("lib").resolve("modules");
 
     Map<Path, FileSystem> fileSystems = new LinkedHashMap<>();
-    List<Closeable> closeables = new ArrayList<>();
+    // List of resources to be closed (self-sychronized).
+    private final List<Closeable> closeables = new ArrayList<>();
     private String releaseVersion = null;
+    private boolean previewMode = false;
 
     Locations() {
         initHandlers();
     }
 
@@ -143,19 +145,30 @@
         } catch (InvalidPathException ipe) {
             throw new IllegalArgumentException(ipe);
         }
     }
 
+    private void addCloseable(Closeable c) {
+        synchronized (closeables) {
+            closeables.add(c);
+        }
+    }
+
     public void close() throws IOException {
         ListBuffer<IOException> list = new ListBuffer<>();
-        closeables.forEach(closeable -> {
+        Closeable[] arr;
+        synchronized (closeables) {
+            arr = closeables.toArray(Closeable[]::new);
+            closeables.clear();
+        }
+        for (Closeable closeable : arr) {
             try {
                 closeable.close();
             } catch (IOException ex) {
                 list.add(ex);
             }
-        });
+        }
         if (list.nonEmpty()) {
             IOException ex = new IOException();
             for (IOException e: list)
                 ex.addSuppressed(e);
             throw ex;
@@ -224,10 +237,15 @@
     public void setMultiReleaseValue(String multiReleaseValue) {
         // Null is implicitly allowed and unsets the value.
         this.releaseVersion = multiReleaseValue;
     }
 
+    public void setPreviewMode(boolean previewMode) {
+        // Null is implicitly allowed and unsets the value.
+        this.previewMode = previewMode;
+    }
+
     private boolean contains(Collection<Path> searchPath, Path file) throws IOException {
 
         if (searchPath == null) {
             return false;
         }
@@ -1457,11 +1475,11 @@
                             try {
                                 Path moduleInfoClass = fs.getPath("classes/module-info.class");
                                 String moduleName = readModuleName(moduleInfoClass);
                                 Path modulePath = fs.getPath("classes");
                                 fileSystems.put(p, fs);
-                                closeables.add(fs);
+                                addCloseable(fs);
                                 fs = null; // prevent fs being closed in the finally clause
                                 return new Pair<>(moduleName, modulePath);
                             } finally {
                                 if (fs != null)
                                     fs.close();
@@ -1948,28 +1966,31 @@
                 try {
                     URI jrtURI = URI.create("jrt:/");
                     FileSystem jrtfs;
 
                     if (isCurrentPlatform(systemJavaHome)) {
-                        jrtfs = FileSystems.getFileSystem(jrtURI);
+                        JRTIndex jrtIndex = JRTIndex.instance(previewMode);
+                        addCloseable(jrtIndex);
+                        jrtfs = jrtIndex.getFileSystem();
                     } else {
                         try {
                             Map<String, String> attrMap =
-                                    Collections.singletonMap("java.home", systemJavaHome.toString());
+                                    Map.of("java.home", systemJavaHome.toString(),
+                                            "previewMode", String.valueOf(previewMode));
                             jrtfs = FileSystems.newFileSystem(jrtURI, attrMap);
                         } catch (ProviderNotFoundException ex) {
                             URL jfsJar = resolveInJavaHomeLib(systemJavaHome, "jrt-fs.jar").toUri().toURL();
                             ClassLoader currentLoader = Locations.class.getClassLoader();
                             URLClassLoader fsLoader =
                                     new URLClassLoader(new URL[] {jfsJar}, currentLoader);
 
                             jrtfs = FileSystems.newFileSystem(jrtURI, Collections.emptyMap(), fsLoader);
 
-                            closeables.add(fsLoader);
+                            addCloseable(fsLoader);
                         }
 
-                        closeables.add(jrtfs);
+                        addCloseable(jrtfs);
                     }
 
                     modules = jrtfs.getPath("/modules");
                 } catch (FileSystemNotFoundException | ProviderNotFoundException e) {
                     modules = resolveInJavaHomeLib(systemJavaHome, "modules");
