/**
*
*/
package iotest.serversocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.GZIPOutputStream;
/**
* @author Brandon B. Lin
*
*/
public class GZipThread extends Thread {
private List<File> jobQueue;
private static int filesCompressed = 0;
public GZipThread(List<File> jobQueue) {
this.jobQueue = jobQueue;
}
private static synchronized void incrementFilesCompressed() {
filesCompressed++;
}
@Override
public void run() {
while (filesCompressed != GZipAllFiles
.getNumbersOfFilesToBeCompressed()) { // if there is any job, even not in jobqueue
File input = getAJobFromQueue();
if(input == null) {
return;
} else {
incrementFilesCompressed();
compressedAnFile(input);
}
}
}
/**
* Get a job from queue if any , null if all job done
*/
private File getAJobFromQueue() {
File result = null;
synchronized (jobQueue) {
while (jobQueue.isEmpty()) {
if (filesCompressed == GZipAllFiles
.getNumbersOfFilesToBeCompressed()) {
System.out.println(Thread.currentThread() + " ending!");
return result;
}
try {
jobQueue.wait();
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
result = jobQueue.remove(jobQueue.size() - 1);
}
return result;
}
/**
* compress an file
*/
private void compressedAnFile(File fileToBeCompressed) {
if (!fileToBeCompressed.getName().endsWith(".gz")) { // 不压缩已经压缩的文件
try {
InputStream in = new BufferedInputStream(new FileInputStream(
fileToBeCompressed));
File output = new File(fileToBeCompressed.getParent(),
fileToBeCompressed.getName() + ".gz");
if (!output.exists()) { // 不重复压缩
OutputStream out = new BufferedOutputStream(
new GZIPOutputStream(new FileOutputStream(output)));
copyIntoOut(in, out);
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
/**
* copy data from in to out
*/
private void copyIntoOut(InputStream in, OutputStream out)
throws IOException {
int readByte;
while ((readByte = in.read()) != -1) {
out.write(readByte);
}
out.flush();
out.close();
in.close();
}
}
/**
*
*/
package iotest.serversocket;
import java.io.File;
import java.util.Vector;
/**
* @author Brandon B. Lin
*
*/
public class GZipAllFiles {
public final static int THREAD_COUNT = 4;
private static int filesToBeCompressed = -1;
private static GZipThread[] threads = new GZipThread[THREAD_COUNT];
private static Vector<File> jobQueue = new Vector<>();
public static void main(String[] args) {
createThreadPool();
String directoryToBeCompressed = "F:\\java\\UML\\";
addFilesToJobQueue(directoryToBeCompressed);
notifyAllThreadNoMoreJobs();
}
/**
* create THREAD_COUNT threads in thread pool
*/
private static void createThreadPool() {
for (int i = 0; i < threads.length; i++) {
threads[i] = new GZipThread(jobQueue);
threads[i].start();
}
}
/**
* if parameter is an file, add the file to jobQueue,
* if parameter is an directory, add first level's uncompressed files to jobqueue
*/
private static void addFilesToJobQueue(String directory) {
File file = new File(directory);
int totalFiles = 0;
if (file.exists()) {
if (file.isDirectory()) {
totalFiles += addDirectoryToJobQueue(file);
} else {
addAnFileToJobQueue(file);
totalFiles++;
}
}
filesToBeCompressed = totalFiles; // 必须一次性增加
}
/**
* 添加目錄的第一級文檔到作業隊列
*/
private static int addDirectoryToJobQueue(File directory) {
File[] files = directory.listFiles();
int numberOfFiles = 0;
for (int j = 0; j < files.length; j++) {
if (!files[j].isDirectory()) {
addAnFileToJobQueue(files[j]);
numberOfFiles++;
}
}
return numberOfFiles;
}
/**
* add an file (not directory) to jobqueuqe
*/
private static void addAnFileToJobQueue(File fileToAdd) {
synchronized (jobQueue) {
jobQueue.add(0, fileToAdd);
jobQueue.notifyAll();
}
}
/**
* Notify all threads in thread pool that no more job will be added.
*/
private static void notifyAllThreadNoMoreJobs() {
for (int i = 0; i < threads.length; i++) {
threads[i].interrupt();
}
}
/**
* How many files need to be compressed
*/
public static int getNumbersOfFilesToBeCompressed() {
return filesToBeCompressed;
}
}
分享到:
相关推荐
[JAVA][线程池]例子
一个关于java 线程池的例子,也适合android
java线程池和反射机制例子
线程池里的线程等待等待队列里的线程执行,等待队列里的等待线程池里的完成,相互等待
java 线程池常用方法参考文档 线程池中各种方法的讲解,使用,例子
执行一个异步任务你还只是如下new Thread吗?是不是太low 了一点? 我这里有四种线程池的案例轻松让你理解和使用线程池。
请最好使用MyEclipse导入工程,或者直接把src中的java文件拷贝到其他工程也可以使用,对想理解线程池概念的同学有很好的帮助
线程池示例(包含自定义拒绝策略),演示了如何创建一个线程池,以及添加到队列的过程,先添加到工作线程,然后是缓存队列,最后是创建临时线程
java 线程池 实现 例子,线程池实例
corePoolSize:核心池的大小,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; ...
java代码 ThreadPoolExecutor线程池并发测试例子如有误欢迎指正
Java 线程池示例 一个简单的 Java 线程池示例
NULL 博文链接:https://x125858805.iteye.com/blog/2191873
是一个java在进行socket编程时,关于线程池的介绍,有代码和例子
实例多 有自己封装的Threadpool 有java自己封装的线程池 测试例子 都有的
Java并发编程常见知识点源码集锦,涉及到对象锁,Executors多任务线程框架,线程池等示例,列出一些源码包中包括的内容: volatile关键字的非原子性、volatile关键字的使用、AtomicInteger原子性操作、线程安全小...
java线程、线程池、xml解析 适合入门的例子或文档 上手使用
主要介绍了Java线程池ForkJoinPool实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收。所以提高服务程序效率的一个手段就是尽可能减少创建和销毁对象的次数,特别是一些很耗资源的对象创建和销毁。如何利用已有对象来...
在最近做的一个项目中,需要大量的使用到多线程和线程池,下面就java自带的线程池和大家一起分享