6640532: Graphics.getFontMetrics() throws NullPointerException
NIO usage needs to be robust against Thread.interrupt() Reviewed-by: tdv
This commit is contained in:
parent
1fd0bb2370
commit
a15c780502
@ -93,7 +93,6 @@ public final class FontManager {
|
|||||||
*/
|
*/
|
||||||
private static final int CHANNELPOOLSIZE = 20;
|
private static final int CHANNELPOOLSIZE = 20;
|
||||||
private static int lastPoolIndex = 0;
|
private static int lastPoolIndex = 0;
|
||||||
private static int poolSize = 0;
|
|
||||||
private static FileFont fontFileCache[] = new FileFont[CHANNELPOOLSIZE];
|
private static FileFont fontFileCache[] = new FileFont[CHANNELPOOLSIZE];
|
||||||
|
|
||||||
/* Need to implement a simple linked list scheme for fast
|
/* Need to implement a simple linked list scheme for fast
|
||||||
@ -283,29 +282,32 @@ public final class FontManager {
|
|||||||
private static native void initIDs();
|
private static native void initIDs();
|
||||||
|
|
||||||
public static void addToPool(FileFont font) {
|
public static void addToPool(FileFont font) {
|
||||||
boolean added = false;
|
|
||||||
|
FileFont fontFileToClose = null;
|
||||||
|
int freeSlot = -1;
|
||||||
|
|
||||||
synchronized (fontFileCache) {
|
synchronized (fontFileCache) {
|
||||||
/* use poolSize to quickly detect if there's any free slots.
|
/* Avoid duplicate entries in the pool, and don't close() it,
|
||||||
* This is a performance tweak based on the assumption that
|
* since this method is called only from within open().
|
||||||
* if this is executed at all often, its because there are many
|
* Seeing a duplicate is most likely to happen if the thread
|
||||||
* fonts being used and the pool will be full, and we will save
|
* was interrupted during a read, forcing perhaps repeated
|
||||||
* a fruitless iteration
|
* close and open calls and it eventually it ends up pointing
|
||||||
|
* at the same slot.
|
||||||
*/
|
*/
|
||||||
if (poolSize < CHANNELPOOLSIZE) {
|
for (int i=0;i<CHANNELPOOLSIZE;i++) {
|
||||||
for (int i=0; i<CHANNELPOOLSIZE; i++) {
|
if (fontFileCache[i] == font) {
|
||||||
if (fontFileCache[i] == null) {
|
return;
|
||||||
fontFileCache[i] = font;
|
}
|
||||||
poolSize++;
|
if (fontFileCache[i] == null && freeSlot < 0) {
|
||||||
added = true;
|
freeSlot = i;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert added;
|
if (freeSlot >= 0) {
|
||||||
|
fontFileCache[freeSlot] = font;
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
// is it possible for this to be the same font?
|
/* replace with new font. */
|
||||||
assert fontFileCache[lastPoolIndex] != font;
|
fontFileToClose = fontFileCache[lastPoolIndex];
|
||||||
/* replace with new font, poolSize is unchanged. */
|
|
||||||
fontFileCache[lastPoolIndex].close();
|
|
||||||
fontFileCache[lastPoolIndex] = font;
|
fontFileCache[lastPoolIndex] = font;
|
||||||
/* lastPoolIndex is updated so that the least recently opened
|
/* lastPoolIndex is updated so that the least recently opened
|
||||||
* file will be closed next.
|
* file will be closed next.
|
||||||
@ -313,6 +315,19 @@ public final class FontManager {
|
|||||||
lastPoolIndex = (lastPoolIndex+1) % CHANNELPOOLSIZE;
|
lastPoolIndex = (lastPoolIndex+1) % CHANNELPOOLSIZE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Need to close the font file outside of the synchronized block,
|
||||||
|
* since its possible some other thread is in an open() call on
|
||||||
|
* this font file, and could be holding its lock and the pool lock.
|
||||||
|
* Releasing the pool lock allows that thread to continue, so it can
|
||||||
|
* then release the lock on this font, allowing the close() call
|
||||||
|
* below to proceed.
|
||||||
|
* Also, calling close() is safe because any other thread using
|
||||||
|
* the font we are closing() synchronizes all reading, so we
|
||||||
|
* will not close the file while its in use.
|
||||||
|
*/
|
||||||
|
if (fontFileToClose != null) {
|
||||||
|
fontFileToClose.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -334,7 +349,6 @@ public final class FontManager {
|
|||||||
for (int i=0; i<CHANNELPOOLSIZE; i++) {
|
for (int i=0; i<CHANNELPOOLSIZE; i++) {
|
||||||
if (fontFileCache[i] == font) {
|
if (fontFileCache[i] == font) {
|
||||||
fontFileCache[i] = null;
|
fontFileCache[i] = null;
|
||||||
poolSize--;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
89
jdk/test/java/awt/font/Threads/FontThread.java
Normal file
89
jdk/test/java/awt/font/Threads/FontThread.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
||||||
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
||||||
|
* have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/* @test
|
||||||
|
* @summary verify thread interruption doesn't affect font file reading.
|
||||||
|
* @bug 6640532
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
|
||||||
|
public class FontThread extends Thread {
|
||||||
|
|
||||||
|
String fontName = "Dialog";
|
||||||
|
static FontThread thread1;
|
||||||
|
static FontThread thread2;
|
||||||
|
static FontThread thread3;
|
||||||
|
|
||||||
|
public static void main(String args[]) throws Exception {
|
||||||
|
thread1 = new FontThread("SansSerif");
|
||||||
|
thread2 = new FontThread("Serif");
|
||||||
|
thread3 = new FontThread("Monospaced");
|
||||||
|
thread1.dometrics(60); // load classes first
|
||||||
|
thread1.start();
|
||||||
|
thread2.start();
|
||||||
|
thread3.start();
|
||||||
|
InterruptThread ithread = new InterruptThread();
|
||||||
|
ithread.setDaemon(true);
|
||||||
|
ithread.start();
|
||||||
|
thread1.join();
|
||||||
|
thread2.join();
|
||||||
|
thread3.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
FontThread(String font) {
|
||||||
|
super();
|
||||||
|
this.fontName = font;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
System.out.println("started "+fontName); System.out.flush();
|
||||||
|
dometrics(4000);
|
||||||
|
System.out.println("done "+fontName); System.out.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dometrics(int max) {
|
||||||
|
Font f = new Font(fontName, Font.PLAIN, 12);
|
||||||
|
FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(f);
|
||||||
|
for (char i=0;i<max;i++) {
|
||||||
|
if (f.canDisplay(i)) fm.charWidth(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class InterruptThread extends Thread {
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(1);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
thread1.interrupt();
|
||||||
|
thread2.interrupt();
|
||||||
|
thread3.interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user