大二的時(shí)候做的課程設(shè)計(jì),圖片管理器,當(dāng)時(shí)遇到圖片很多的文件夾,加載順序非常慢。雖然嘗試用多個(gè)Thread加載圖片,卻無法保證圖片按順序加載。直到今天學(xué)會(huì)了使用Callable接口和Future接口,于是心血來潮實(shí)現(xiàn)了這個(gè)功能。
十多年的麻山網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷型網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整麻山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“麻山網(wǎng)站設(shè)計(jì)”,“麻山網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
廢話不多說,看代碼。
多線程加載圖片(核心):
package com.lin.imagemgr;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import net.coobird.thumbnailator.Thumbnails;
public class ImageMgr {
private static ImageMgr instance = new ImageMgr();
private ImageMgr() {}
public static ImageMgr getInstance() {
return instance;
}
//線程池
private ExecutorService executor = Executors.newFixedThreadPool(8);
public List<JLabel> loadImages(String path) {
List<JLabel> images = new ArrayList<>();
File file = new File(path);
if (!file.isDirectory()) {
throw new RuntimeException("need directory!");
}
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
//thumbnail只支持jpg??
if (name.endsWith(".jpg")) {
return true;
}
return false;
}
});
//并發(fā)加載圖片,并使用Future保存加載結(jié)果
List<Future<MyLabel>> futures = new ArrayList<>();
for (final File f : files) {
Future<MyLabel> future = executor.submit(() -> {
return new MyLabel(f.getName(), f.getAbsolutePath());
});
futures.add(future);
}
//等待所有并發(fā)加載返回結(jié)果
try {
for (Future<MyLabel> future : futures) {
MyLabel icon = future.get();
images.add(icon);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
//Java8使用stream API 進(jìn)行排序
List<JLabel> sortedList = images.stream().sorted().collect(Collectors.toList());
return sortedList;
}
//繼承JLabel并實(shí)現(xiàn)Comparable接口,從而對(duì)JLabel進(jìn)行排序
private static class MyLabel extends JLabel implements Comparable<MyLabel>{
private static final long serialVersionUID = 1L;
private String fileName;
public MyLabel(String fileName, String fullPath) {
this.fileName = fileName;
//使用thumbnailator生成縮略圖
try {
BufferedImage bufferedImage = Thumbnails.of(fullPath)
.size(100, 120)
.asBufferedImage();
setIcon(new ImageIcon(bufferedImage));
setPreferredSize(new Dimension(100, 120));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public int compareTo(MyLabel o) {
int result = this.fileName.compareTo(o.fileName);
return result;
}
}
}
Swing界面:
package com.lin.imagemgr;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class MainFrame extends JFrame{
private static final long serialVersionUID = 1L;
private JTextField pathField;
private JButton showBtn;
private JPanel contentPanel;
public void init() {
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
topPanel.setPreferredSize(new Dimension(800, 40));
pathField = new JTextField(50);
showBtn = new JButton("顯示圖片");
topPanel.add(pathField);
topPanel.add(showBtn);
getContentPane().add(BorderLayout.NORTH, topPanel);
contentPanel = new JPanel();
contentPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
contentPanel.setPreferredSize(new Dimension(750, 1800));
JScrollPane jsp = new JScrollPane(contentPanel);
getContentPane().add(BorderLayout.CENTER, jsp);
showBtn.addActionListener((e) -> {
try {
loadImages();
} catch (Exception ex) {
ex.printStackTrace();
}
});
setSize(800, 650);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public void loadImages() {
contentPanel.removeAll();
String path = pathField.getText();
long start = System.currentTimeMillis();
List<JLabel> images = ImageMgr.getInstance().loadImages(path);
for (JLabel label :images) {
contentPanel.add(label);
}
contentPanel.updateUI();
long end = System.currentTimeMillis();
System.out.println("加載需要" + (end - start) + "毫秒!");
}
public static void main(String[] args) {
new MainFrame().init();
}
}
運(yùn)行結(jié)果


在我的電腦上,加載92張圖片并渲染到界面上,總共花了1568毫秒。大家可以找一個(gè)圖片很多的文件夾,嘗試加載大量圖片的情況。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:JavaSwing多線程加載圖片(保證順序一致)
本文來源:http://chinadenli.net/article34/jgjhse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、App開發(fā)、企業(yè)建站、虛擬主機(jī)、云服務(wù)器、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)