Extras: fixed concurrent loading of SVG icons on multiple threads (issue #459)

This commit is contained in:
Karl Tauber
2021-12-30 11:24:48 +01:00
parent bb32c727b6
commit 0cdfd29ecf
3 changed files with 36 additions and 3 deletions

View File

@@ -17,6 +17,8 @@ FlatLaf Change Log
- Improved hover/pressed/selected colors of leading/trailing buttons (e.g.
"reveal" button in password field). (issue #452)
- Clear button no longer paints over round border. (issue #451)
- Extras: Fixed concurrent loading of SVG icons on multiple threads. (issue
#459)
## 2.0-rc1

View File

@@ -273,10 +273,12 @@ public class FlatSVGIcon
// since the input stream is already loaded and parsed,
// get diagram here and remove it from cache
update();
synchronized( FlatSVGIcon.class ) {
svgCache.remove( uri );
}
}
private static URI loadFromStream( InputStream in ) throws IOException {
private static synchronized URI loadFromStream( InputStream in ) throws IOException {
try( InputStream in2 = in ) {
return svgUniverse.loadSVG( in2, "/flatlaf-stream-" + (streamNumber++) );
}
@@ -475,7 +477,7 @@ public class FlatSVGIcon
loadFailed = (diagram == null);
}
static SVGDiagram loadSVG( URI uri ) {
static synchronized SVGDiagram loadSVG( URI uri ) {
// get from our cache
SVGDiagram diagram = svgCache.get( uri );
if( diagram != null )

View File

@@ -27,6 +27,7 @@ import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import com.formdev.flatlaf.FlatClientProperties;
import com.formdev.flatlaf.FlatLightLaf;
import com.formdev.flatlaf.extras.FlatSVGIcon;
/**
* @author Karl Tauber
@@ -57,6 +58,7 @@ public class FlatStressTest
private JComponent createStressTest() {
return createComboBoxStressTest();
// return createGetFontStressTest();
// return createSVGIconTest();
}
// for https://github.com/JFormDesigner/FlatLaf/issues/432
@@ -109,4 +111,31 @@ public class FlatStressTest
return label;
}
// for https://github.com/JFormDesigner/FlatLaf/issues/459
@SuppressWarnings( "unused" )
private JComponent createSVGIconTest() {
JLabel label = new JLabel( "test" );
Runnable runnable = () -> {
for(;;) {
FlatSVGIcon icon = new FlatSVGIcon( "com/formdev/flatlaf/demo/icons/back.svg" );
icon.getIconHeight();
}
};
Thread thread1 = new Thread( runnable);
thread1.setDaemon( true );
thread1.start();
Thread thread2 = new Thread( runnable);
thread2.setDaemon( true );
thread2.start();
Thread thread3 = new Thread( runnable);
thread3.setDaemon( true );
thread3.start();
return label;
}
}