内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

JavaSwing多線程加載圖片(保證順序一致)

大二的時候做的課程設(shè)計,圖片管理器,當(dāng)時遇到圖片很多的文件夾,加載順序非常慢。雖然嘗試用多個Thread加載圖片,卻無法保證圖片按順序加載。直到今天學(xué)會了使用Callable接口和Future接口,于是心血來潮實現(xiàn)了這個功能。

十多年的麻山網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整麻山建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)公司從事“麻山網(wǎng)站設(shè)計”,“麻山網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(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并實現(xiàn)Comparable接口,從而對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();
 }

}

運行結(jié)果

Java Swing 多線程加載圖片(保證順序一致)

Java Swing 多線程加載圖片(保證順序一致)

在我的電腦上,加載92張圖片并渲染到界面上,總共花了1568毫秒。大家可以找一個圖片很多的文件夾,嘗試加載大量圖片的情況。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站欄目:JavaSwing多線程加載圖片(保證順序一致)
本文來源:http://www.rwnh.cn/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)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化
遂溪县| 华容县| 寿宁县| 温州市| 新巴尔虎左旗| 佳木斯市| 沁阳市| 绥江县| 布拖县| 广丰县| 阳西县| 右玉县| 安陆市| 格尔木市| 武功县| 宁海县| 朔州市| 洮南市| 当阳市| 永兴县| 类乌齐县| 晴隆县| 南岸区| 铁力市| 凌海市| 阿尔山市| 乌兰县| 咸丰县| 临颍县| 儋州市| 韶山市| 盐山县| 屯昌县| 禹州市| 和顺县| 渑池县| 抚州市| 泸水县| 嘉峪关市| 额尔古纳市| 美姑县|