Path: blob/master/test/jdk/javax/imageio/plugins/jpeg/ConcurrentWritingTest.java
41152 views
/*1* Copyright (c) 2007, 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* @bug 654724126* @summary Test verifies that concurrent usage of jpeg writer instance27* by number of threads does not cause crash in jpeg library.28* @run main ConcurrentWritingTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.RadialGradientPaint;34import java.awt.geom.Point2D;35import java.awt.image.BufferedImage;36import java.io.File;37import java.io.IOException;38import javax.imageio.ImageIO;39import javax.imageio.ImageWriter;40import javax.imageio.stream.ImageOutputStream;4142public class ConcurrentWritingTest extends Thread {4344static ImageWriter w = null;45static File pwd = new File(".");46static BufferedImage img;4748private static int MAX_THREADS = 50;49private static int completeCount = 0;50private static Object lock = new Object();5152public static void main(String[] args) throws Exception {53img = createTestImage();5455w = ImageIO.getImageWritersByFormatName("JPEG").next();5657for (int i = 0; i < MAX_THREADS; i++) {58(new ConcurrentWritingTest()).start();59}6061// wait for threads62boolean needWait = true;63while(needWait) {64synchronized(lock) {65needWait = completeCount < MAX_THREADS;66}67}68System.out.println("Test PASSED");69}7071public void run() {72try {73File f = File.createTempFile("writer_", ".jpg", pwd);74ImageOutputStream ios = ImageIO.createImageOutputStream(f);75w.setOutput(ios);76Thread.sleep(70);77w.write(img);78Thread.sleep(70);79w.reset();80} catch (IllegalStateException e) {81System.out.println(e);82} catch (IOException e) {83System.out.println(e);84} catch (Throwable e) {85// Unexpected exception. Test failed.86throw new RuntimeException("Test failed.", e);87} finally {88synchronized(lock) {89completeCount ++;90}91}92}9394private static BufferedImage createTestImage() {95int w = 1024;96int h = 768;9798BufferedImage img = new99BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);100Graphics2D g = img.createGraphics();101Color[] colors = { Color.red, Color.green, Color.blue };102float[] dist = {0.0f, 0.5f, 1.0f };103Point2D center = new Point2D.Float(0.5f * w, 0.5f * h);104105RadialGradientPaint p =106new RadialGradientPaint(center, 0.5f * w, dist, colors);107g.setPaint(p);108g.fillRect(0, 0, w, h);109g.dispose();110111return img;112}113}114115116