簡單的多執行緒複製檔案
Demo.class
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
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.concurrent.atomic.AtomicInteger;
public class demo {
private static String sourceRootPath = "D:\\source-photo\\";
private static String toRootPath = "D:\\compressed-photo\\";
private static AtomicInteger count = new AtomicInteger(2);
private static AtomicInteger index = new AtomicInteger(0);
private static final int workSize = 8;
public static void main(String[] args) {
/*try {
copyFiles(1000);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
try {
compressPhotoFiles();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void compressPhotoFiles() throws InterruptedException {
// TODO 檢查路徑是否合法
// 這裡需要修改為返回檔案路徑,返回檔案會造成記憶體溢位
List<File> list = new ArrayList<>(FileUtils.listFiles(new File(sourceRootPath),new String[]{"png","jpg"},true));
Calendar start = Calendar.getInstance();
ExecutorService executor = Executors.newFixedThreadPool(workSize);
for(int i=0;i<=workSize;i++){
executor.submit(new CompressPhotoRunnable(list,toRootPath,index));
}
executor.shutdown();
while(true){
if (executor.isTerminated()) {
double minute = (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis())/1000/60.0;
System.out.println("圖片壓縮完成:"+minute+"分鐘");
break;
}
Thread.currentThread().sleep(1000);
}
}
public static void copyFiles(int size) throws ExecutionException, InterruptedException {
Calendar start = Calendar.getInstance();
ExecutorService executor = Executors.newFixedThreadPool(workSize);
ArrayList<Boolean> list = new ArrayList<>();
for (int i=1;i<=workSize;i++){
Future<Boolean> isOk = executor.submit(new CopyFileCallable(sourceRootPath,i,count,size));
list.add((Boolean) isOk.get());
}
System.out.println(list.toString());
executor.shutdown();
while(true){
if (executor.isTerminated()) {
double minute = (Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis())/1000/60.0;
System.out.println("圖片複製完成:"+minute+"分鐘");
break;
}
Thread.currentThread().sleep(1000);
}
}
}
執行緒類CopyFileRunnable,建立大量圖片用於測試
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicInteger;
public class CopyFileCallable implements Callable {
private String fromPath;
private Integer index ;
private AtomicInteger count;
private static Integer size;
public CopyFileCallable(String fromPath,Integer index,AtomicInteger count,Integer size){
this.fromPath = fromPath;
this.index = index;
this.count = count;
this.size = size;
}
@Override
public Boolean call() throws Exception {
String fromPath = this.fromPath + index + ".jpg";
int addCount = 0;
File from = new File(fromPath);
while(!from.exists()){
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
from = new File(fromPath);
}
while((addCount=count.getAndAdd(1))<=size && from.canRead()){
System.out.println(addCount);
String toPath = fromPath + addCount + ".jpg";
File to = new File(toPath);
try {
if(!to.exists()){
Files.copy(from.toPath(),to.toPath());
// System.out.println("圖片執行緒"+i+"-"+(addCount+1)+" 已建立");
}
} catch (IOException e) {
// e.printStackTrace();
System.out.println("檔案"+addCount+".jpg 複製失敗:"+e.getMessage());
}
to = null;
}
return true;
}
}
壓縮圖片執行緒類
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.io.FileUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class CompressPhotoRunnable implements Runnable{
private List<File> files;
private String toPath;
private AtomicInteger index;
public CompressPhotoRunnable(List<File> files, String toPath, AtomicInteger index) {
this.files = files;
this.toPath = toPath;
this.index = index;
}
@Override
public void run() {
int nowIndex = 0;
File file;
double scale = 1;
int min;
while ((nowIndex=index.getAndAdd(1))<=files.size()){
System.out.println(nowIndex);
try {
file = files.get(nowIndex);
BufferedImage buff = ImageIO.read(file);
if((min = Math.min(buff.getHeight(),buff.getWidth()))>500){
// 小的邊根據比例壓縮到500畫素
scale = Math.round((500.00/min*100))/100.00;
}
Thumbnails.of(buff).scale(scale).toFile(toPath+file.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
相關文章
- delphi多執行緒檔案複製怎麼實現執行緒
- MySQL併發複製系列二:多執行緒複製MySql執行緒
- wxWidgets簡單的多執行緒執行緒
- 多執行緒Demo學習(執行緒的同步,簡單的執行緒通訊)執行緒
- 多執行緒下載檔案執行緒
- Golang多執行緒簡單鬥地主Golang執行緒
- 多執行緒 -- 初學簡單例子執行緒單例
- 多執行緒統計多個檔案的單詞數目---C++0x多執行緒使用示例執行緒C++
- 多執行緒複習執行緒
- MySQL 主從複製之多執行緒複製MySql執行緒
- ObjC 多執行緒簡析(一)-多執行緒簡述和執行緒鎖的基本應用OBJ執行緒
- Java多執行緒就是這麼簡單Java執行緒
- LINUX 如何實現多執行緒進行cp複製Linux執行緒
- 【Java】多執行緒複習Java執行緒
- 【Java】【多執行緒】執行緒池簡述Java執行緒
- 跨網路複製檔案的簡單實踐
- 簡單的執行緒池執行緒
- Mysql複製執行緒狀態MySql執行緒
- 多執行緒賣火車票簡單例子執行緒單例
- 【多執行緒系列】CAS、AQS簡單介紹執行緒AQS
- java多執行緒 wait() notify()簡單使用Java執行緒AI
- Java簡單多執行緒斷點下載Java執行緒斷點
- 多執行緒之死鎖就是這麼簡單執行緒
- Python簡單實現多執行緒例子Python執行緒
- MySQL Replication的複製執行緒介紹MySql執行緒
- 如何批量複製多個檔案到多個目錄中(批量複製檔案,多對多檔案高效操作的方法)
- 多執行緒下的單例執行緒單例
- Python多執行緒併發的簡單測試Python執行緒
- linux程式多執行緒互斥鎖的簡單使用Linux執行緒
- 簡單的執行緒池(八)執行緒
- 簡單的執行緒池(七)執行緒
- 簡單的執行緒池(九)執行緒
- 簡單的執行緒池(六)執行緒
- 簡單的執行緒池(四)執行緒
- 簡單的執行緒池(三)執行緒
- 簡單的執行緒池(二)執行緒
- 【多執行緒】常見問題簡單總結執行緒
- python 多執行緒就這麼簡單(續)Python執行緒