文档章节

High memory usage with ExecutorService.

pczhangtl
 pczhangtl
发布于 2014/09/05 18:05
字数 416
阅读 19
收藏 0
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import javax.imageio.ImageIO;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingWorker;
import org.imgscalr.Scalr;

public class ImageThumbList extends JFrame {
	private final int THUMB_WIDTH;
	private final int THUMB_HEIGHT;
	private final int HORIZONTAL_SPACING;
	private final int VERTICAL_SPACING;
	private final Color BACKGROUND;
	private final String IMAGES_DIR_PATH;
	private final double THUMB_RATIO;
	private LinkedBlockingQueue<File> queue;
	private boolean useSwingWorker;
	private JList iconJList;
	private JScrollPane iconJListScrollPane;
	private DefaultListCellRenderer iconJListRenderer;
	private DefaultListModel iconJListModel;

	public ImageThumbList() {
		THUMB_WIDTH = 174;
		THUMB_HEIGHT = 174;
		HORIZONTAL_SPACING = 10;
		VERTICAL_SPACING = 10;
		IMAGES_DIR_PATH = ImagePath;
		THUMB_RATIO = (double) THUMB_WIDTH / (double) THUMB_HEIGHT;
		BACKGROUND = new Color(Integer.parseInt("646464", 16));
		useSwingWorker = true;
		initComponents();
	}

	private void initComponents() {
		setLayout(new BorderLayout());
		iconJListModel = new DefaultListModel();
		iconJList = new JList(iconJListModel);
		iconJListRenderer = (DefaultListCellRenderer) iconJList
				.getCellRenderer();
		iconJListScrollPane = new JScrollPane(iconJList);
		iconJList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		iconJList.setFixedCellHeight(THUMB_HEIGHT + VERTICAL_SPACING);
		iconJList.setFixedCellWidth(THUMB_WIDTH + HORIZONTAL_SPACING);
		iconJList.setBackground(BACKGROUND);
		iconJList.setVisibleRowCount(-1);
		iconJListRenderer.setHorizontalAlignment(JLabel.CENTER);
		iconJListRenderer.setVerticalAlignment(JLabel.CENTER);
		iconJListScrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add(iconJListScrollPane, BorderLayout.CENTER);
	}

	public void start() {
		queue = new LinkedBlockingQueue<File>();
		queue.addAll(Arrays.asList(new File(IMAGES_DIR_PATH)
				.listFiles(new FileFilter() {
					@Override
					public boolean accept(File dir) {
						return dir.isFile();
					}
				})));

		if (useSwingWorker) {
			new SimpleThumbnailGenerator().execute();
		} else {
			new Thread(new ExecutorServiceThumbnailGenerator()).start();
		}
	}

	private class SimpleThumbnailGenerator extends
			SwingWorker<Void, BufferedImage> {
		@Override
		public Void doInBackground() {
			while (!isCancelled()) {
				File file = null;
				try {
					file = queue.take();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				publish(getThumbnail(file));
			}
			return null;
		}

		@Override
		public void process(List<BufferedImage> lista) {
			for (BufferedImage c : lista) {
				if (c != null) {
					iconJListModel.addElement(new ImageIcon(c));
				}
			}
		}
	}

	private class ExecutorServiceThumbnailGenerator implements Runnable {
		private ExecutorService pool;

		public ExecutorServiceThumbnailGenerator() {
			pool = Executors.newFixedThreadPool(Runtime.getRuntime()
					.availableProcessors());
		}

		@Override
		public void run() {
			try {
				while (true) {
					pool.execute(new ThumbnailGenerator(queue.take()));
				}
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			} finally {
				pool.shutdown();
			}
		}
	}

	private class ThumbnailGenerator extends SwingWorker<Void, BufferedImage> {
		File file;

		ThumbnailGenerator(File file) {
			this.file = file;
		}

		@Override
		public Void doInBackground() {
			publish(getThumbnail(file));
			return null;
		}

		@Override
		public void process(List<BufferedImage> lista) {
			for (BufferedImage c : lista) {
				if (c != null) {
					iconJListModel.addElement(new ImageIcon(c));
				}
			}
		}
	}

	private BufferedImage getThumbnail(File file) {
		BufferedImage thumbnail = null;
		if (file != null) {
			try {
				thumbnail = ImageIO.read(file);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (thumbnail != null) {
				int thumbWidth = THUMB_WIDTH;
				int thumbHeight = THUMB_HEIGHT;
				// determine thumbnail size from WIDTH and HEIGHT
				double imageRatio = (double) thumbnail.getWidth(null)
						/ (double) thumbnail.getHeight(null);
				if (THUMB_RATIO < imageRatio) {
					thumbHeight = (int) (thumbWidth / imageRatio);
				} else {
					thumbWidth = (int) (thumbHeight * imageRatio);
				}
				thumbnail = Scalr.resize(thumbnail, thumbWidth, thumbHeight);
			}
		}
		return thumbnail;
	}

	public static void main(String[] args) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ImageThumbList frame = new ImageThumbList();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setSize(800, 445);
				frame.setPreferredSize(new Dimension(800, 445));
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				frame.start();
			}
		});
	}
}



本文转载自:http://www.coderanch.com/t/598612/threads/java/High-memory-usage-ExecutorService

共有 人打赏支持
pczhangtl
粉丝 46
博文 707
码字总数 113318
作品 0
浦东
高级程序员
私信 提问
Hardware Considerations

NewHardware Bling Bling Updated Aug 22, 2011 Hardware Requirements Memcached is easy to spec out hardware for. In short, it is generally low on CPU usage, will take as much memo......

Koma
2016/04/13
0
0
一篇关于“java.lang.OutOfMemoryError: PermGen space”的资料

Explaining java.lang.OutOfMemoryError: PermGen space Most probably, a lot of Java developers have seen OutOfMemory error one time or other. However these errors come in differen......

很懒的人
2010/02/24
1K
0
Java 8 - From PermGen to Metaspace

Java 8 - From PermGen to Metaspace http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html http://ifeve.com/java-permgen-removed/ https://blogs.oracle.com/poonam/abo......

秋风醉了
2014/10/26
1K
0
18 Command Line Tools to Monitor Linux Performance

It’s really very tough job for every System or Network administrator to monitor and debug Linux System Performance problems every day. After being a Linux Administrator for 5 y......

wuyaoito
2014/02/18
0
0
RabbitMQ常用 rabbitmqctl 命令

可参考:http://www.rabbitmq.com/man/rabbitmqctl.1.man.html#idp16080160 用户管理参考:http://www.rabbitmq.com/man/rabbitmqctl.1.man.html#idp17598608 Usage: rabbitmqctl [-q] [-n ......

nibilly
2015/04/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

声学所实验室的论文

https://hccl.ioa.ac.cn/output/papers/

shengjuntu
14分钟前
0
0
Mysql(Mariadb)数据库主从复制

Mysql主从复制的实现原理图大致如下: MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据库中所有操作都...

linux-tao
今天
2
0
Mysql(Mariadb)数据库主从复制

Mysql主从复制的实现原理图大致如下: MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据库中所有操作都...

Linux就该这么学
今天
2
0
Mysql(Mariadb)数据库主从复制

Mysql主从复制的实现原理图大致如下: MySQL之间数据复制的基础是以二进制日志文件(binary log file)来实现的,一台MySQL数据库一旦启用二进制日志后,其作为master,它数据库中所有操作都...

xiangyunyan
今天
2
0
Android 自定义Path贝塞尔曲线View实践——旋转的花朵

一、关于贝塞尔曲线 在工业设计方面贝塞尔曲线有很多用途,同样,在Android中,贝塞尔曲线结合Path类可以实现更复杂的图形,这里我们给一个案例,来实现一种旋转的花朵。对于贝赛尔曲线的理解...

IamOkay
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部