Skip to content

Commit 0982bdd

Browse files
committed
8372048: Performance improvement on Linux remote desktop
Backport-of: 0b3df489e9d3b6d876a67793e082b930c17ade3e
1 parent d5dc445 commit 0982bdd

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/java.desktop/unix/classes/sun/awt/screencast/ScreencastHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ public class ScreencastHelper {
6666
private static final int DELAY_BEFORE_SESSION_CLOSE = 2000;
6767

6868
private static volatile TimerTask timerTask = null;
69-
private static final Timer timerCloseSession
70-
= new Timer("auto-close screencast session", true);
7169

70+
private static class TimerHolder {
71+
private static final Timer timerCloseSession =
72+
new Timer("auto-close screencast session", true);
73+
}
7274

7375
private ScreencastHelper() {}
7476

@@ -152,7 +154,7 @@ public void run() {
152154
}
153155
};
154156

155-
timerCloseSession.schedule(timerTask, DELAY_BEFORE_SESSION_CLOSE);
157+
TimerHolder.timerCloseSession.schedule(timerTask, DELAY_BEFORE_SESSION_CLOSE);
156158
}
157159

158160
public static synchronized void getRGBPixels(

src/java.desktop/unix/classes/sun/awt/screencast/TokenStorage.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ public void run() {
252252
}
253253

254254
private static WatchService watchService;
255+
private static volatile boolean isWatcherThreadStarted = false;
255256

256257
private static void setupWatch() {
257258
doPrivilegedRunnable(() -> {
@@ -273,10 +274,6 @@ private static void setupWatch() {
273274
}
274275
}
275276
});
276-
277-
if (watchService != null) {
278-
new WatcherThread(watchService).start();
279-
}
280277
}
281278

282279
// called from native
@@ -353,7 +350,27 @@ private static boolean readTokens(Path path) {
353350
return true;
354351
}
355352

353+
private static void startWatcherThreadIfNeeded() {
354+
if (!isWatcherThreadStarted) {
355+
// not sure if the double-checked locking is actually needed here
356+
// the getTokens is only called from ScreencastHelper#getRGBPixels
357+
// and ScreencastHelper#remoteDesktop* methods (which are synchronized),
358+
// but it may change later.
359+
synchronized (TokenStorage.class) {
360+
if (!isWatcherThreadStarted) {
361+
readTokens(PROPS_PATH);
362+
if (watchService != null) {
363+
new WatcherThread(watchService).start();
364+
}
365+
isWatcherThreadStarted = true;
366+
}
367+
}
368+
}
369+
}
370+
356371
static Set<TokenItem> getTokens(List<Rectangle> affectedScreenBounds) {
372+
startWatcherThreadIfNeeded();
373+
357374
// We need an ordered set to store tokens
358375
// with exact matches at the beginning.
359376
LinkedHashSet<TokenItem> result = new LinkedHashSet<>();

0 commit comments

Comments
 (0)