ConcurrentWritingTest.java revision 0
3112N/A/*
3112N/A * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
3112N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3112N/A *
3112N/A * This code is free software; you can redistribute it and/or modify it
3112N/A * under the terms of the GNU General Public License version 2 only, as
3112N/A * published by the Free Software Foundation.
3112N/A *
3112N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3112N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3112N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3112N/A * version 2 for more details (a copy is included in the LICENSE file that
3112N/A * accompanied this code).
3112N/A *
3112N/A * You should have received a copy of the GNU General Public License version
3112N/A * 2 along with this work; if not, write to the Free Software Foundation,
3112N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3112N/A *
3112N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3112N/A * CA 95054 USA or visit www.sun.com if you need additional information or
3112N/A * have any questions.
3112N/A */
3112N/A
5061N/A/**
3112N/A * @test
4458N/A * @bug 6547241
4458N/A * @summary Test verifies that concurrent usage of jpeg writer instance
4458N/A * by number of threads does not cause crash in jpeg library.
4458N/A * @run main ConcurrentWritingTest
3832N/A */
3832N/A
4458N/Aimport java.awt.Color;
3832N/Aimport java.awt.Graphics2D;
3832N/Aimport java.awt.RadialGradientPaint;
4458N/Aimport java.awt.geom.Point2D;
4974N/Aimport java.awt.image.BufferedImage;
4714N/Aimport java.io.File;
4458N/Aimport java.io.IOException;
4714N/Aimport javax.imageio.ImageIO;
4251N/Aimport javax.imageio.ImageWriter;
4714N/Aimport javax.imageio.stream.ImageOutputStream;
4714N/A
4251N/Apublic class ConcurrentWritingTest extends Thread {
4978N/A
4978N/A static ImageWriter w = null;
5042N/A static File pwd = new File(".");
4978N/A static BufferedImage img;
5010N/A
4978N/A private static int MAX_THREADS = 50;
3832N/A private static int completeCount = 0;
4458N/A private static Object lock = new Object();
4458N/A
4458N/A public static void main(String[] args) throws Exception {
4458N/A img = createTestImage();
4458N/A
4458N/A w = ImageIO.getImageWritersByFormatName("JPEG").next();
4458N/A
4495N/A for (int i = 0; i < MAX_THREADS; i++) {
4495N/A (new ConcurrentWritingTest()).start();
4495N/A }
4495N/A
4495N/A // wait for threads
4495N/A boolean needWait = true;
4458N/A while(needWait) {
4458N/A synchronized(lock) {
4458N/A needWait = completeCount < MAX_THREADS;
4458N/A }
4458N/A }
4714N/A System.out.println("Test PASSED");
4714N/A }
4714N/A
4458N/A public void run() {
4458N/A try {
4458N/A File f = File.createTempFile("writer_", ".jpg", pwd);
4495N/A ImageOutputStream ios = ImageIO.createImageOutputStream(f);
4458N/A w.setOutput(ios);
4458N/A Thread.sleep(70);
4458N/A w.write(img);
4458N/A Thread.sleep(70);
4458N/A w.reset();
4458N/A } catch (IllegalStateException e) {
4723N/A System.out.println(e);
4458N/A } catch (IOException e) {
4458N/A System.out.println(e);
4458N/A } catch (Throwable e) {
4458N/A // Unexpected exception. Test failed.
4458N/A throw new RuntimeException("Test failed.", e);
4458N/A } finally {
4458N/A synchronized(lock) {
880N/A completeCount ++;
4714N/A }
4714N/A }
4714N/A }
4714N/A
3832N/A private static BufferedImage createTestImage() {
4458N/A int w = 1024;
5061N/A int h = 768;
4458N/A
4458N/A BufferedImage img = new
4458N/A BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
4458N/A Graphics2D g = img.createGraphics();
4458N/A Color[] colors = { Color.red, Color.green, Color.blue };
5061N/A float[] dist = {0.0f, 0.5f, 1.0f };
5061N/A Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);
900N/A
4458N/A RadialGradientPaint p =
900N/A new RadialGradientPaint(center, 0.5f * w, dist, colors);
927N/A g.setPaint(p);
900N/A g.fillRect(0, 0, w, h);
5043N/A g.dispose();
4495N/A
4714N/A return img;
4458N/A }
4865N/A}
4495N/A