Path: blob/master/test/jdk/javax/swing/JFileChooser/6713352/bug6713352.java
41153 views
/*1* Copyright (c) 2009, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 671335227* @summary Deadlock in JFileChooser with synchronized custom FileSystemView28* @author Pavel Porvatov29* @modules java.desktop/sun.awt.shell30* @run main bug671335231*/3233import sun.awt.shell.ShellFolder;3435import javax.swing.*;36import javax.swing.filechooser.FileSystemView;37import java.io.File;38import java.io.FileNotFoundException;39import java.io.IOException;4041public class bug6713352 {42public static void main(String[] args) throws Exception {43SwingUtilities.invokeAndWait(new Runnable() {44public void run() {45String tempDir = System.getProperty("java.io.tmpdir");4647if (tempDir == null || !new File(tempDir).isDirectory()) {48tempDir = System.getProperty("user.home");49}5051MyFileSystemView systemView = new MyFileSystemView();5253synchronized (systemView) { // Get SystemView lock54new JFileChooser(systemView);5556// Wait a little bit. BasicDirectoryModel will lock Invoker and stop on57// the bug6713352.MyFileSystemView.getFiles() method58try {59Thread.sleep(5000);60} catch (InterruptedException e) {61throw new RuntimeException(e);62}6364try {65System.out.println("Try to get Invokers lock");6667ShellFolder.getShellFolder(new File(tempDir)).listFiles(true);68} catch (FileNotFoundException e) {69throw new RuntimeException(e);70}71}7273// To avoid RejectedExecutionException in BasicDirectoryModel wait a second74try {75Thread.sleep(1000);76} catch (InterruptedException e) {77throw new RuntimeException(e);78}79}80});81}8283private static class MyFileSystemView extends FileSystemView {8485public File createNewFolder(File containingDir) throws IOException {86return null;87}8889public File[] getFiles(File dir, boolean useFileHiding) {90System.out.println("getFiles start");9192File[] result;9394synchronized (this) {95result = super.getFiles(dir, useFileHiding);96}9798System.out.println("getFiles finished");99100return result;101}102103public synchronized Boolean isTraversable(File f) {104return super.isTraversable(f);105}106}107}108109110