High memory usage with ExecutorService.

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();
			}
		});
	}
}



转载于:https://my.oschina.net/u/138995/blog/310693

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package 操作系统实验二; import java.util.concurrent.*; class PrintTask implements Runnable { private char ch; private Semaphore currentSemaphore; private Semaphore nextSemaphore; public PrintTask(char ch, Semaphore currentSemaphore, Semaphore nextSemaphore) { this.ch = ch; this.currentSemaphore = currentSemaphore; this.nextSemaphore = nextSemaphore; } public void run() { try { for (int i = 0; i < 3; i++) { currentSemaphore.acquire(); System.out.print(ch); nextSemaphore.release(); } } catch (InterruptedException e) { e.printStackTrace(); } } } public class Main { public static void main(String[] args) { Semaphore semaphoreA = new Semaphore(1); Semaphore semaphoreB = new Semaphore(0); PrintTask printA = new PrintTask('A', semaphoreA, semaphoreB); PrintTask printB = new PrintTask('B', semaphoreB, semaphoreA); ExecutorService executorService = Executors.newFixedThreadPool(2); // 顺序1:AAAB executorService.execute(printA); executorService.execute(printA); executorService.execute(printA); executorService.execute(printB); // 顺序2:BBAA //executorService.execute(printB); //executorService.execute(printB); //executorService.execute(printA); //executorService.execute(printA); // 顺序3:AABA //executorService.execute(printA); //executorService.execute(printA); //executorService.execute(printB); //executorService.execute(printA); // 顺序4:ABAA //executorService.execute(printA); //executorService.execute(printB); //executorService.execute(printA); //executorService.execute(printA); executorService.shutdown(); } }
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值