Java multi-threaded upload Extract files

Source: Internet
Author: User
Tags file copy

For a company project development encountered a simple example, the user uploads the compressed file to the server, to the compressed package two operations, one is to copy the package to the specified directory, one is to extract the package to another specified directory, the final response to the user prompt file upload success. If the compression package is large, the copy and decompression function after uploading will take a long time and the user will wait for a long time. In fact, the function of replication and decompression is not directly related to user operation, it can be independently, and its solution is as follows:
When the user uploads the compressed file, we immediately create two threads, one to copy the compressed file, and the other to unzip the compressed file. We can pass the file information to the corresponding thread through the thread construction method, when we start the two thread's Start method, we no longer care about its copy and decompression operation, but directly respond to the user, so the user obviously feel the operation is faster, and the copy and decompression operation is still secretly in the background.

There are two ways to implement multithreading, one is to inherit thread, and the other is to implement Interface runnable. The difference between the two is that inheritance can only be inherited, and the interface can be implemented more than one, so I prefer to use the latter.

The code model is posted below for your reference:

[Java]
Package com.yjd.test;

Import Java.io.File;

public class Fileoperate {
public static void Main (string[] args) {
Long begin = System.currenttimemillis ();

Uploading files
UploadFile uploadfile = new UploadFile ();
File File = Uploadfile.uploadfilemethod ();
Passing parameters to a thread
Coppyfile coppyfile = new Coppyfile (file);
Unzipfile unzipfile = new Unzipfile (file);
Creating Threads
Thread coppythread = new Thread (coppyfile);
Thread unzipthread = new Thread (unzipfile);
Start thread
Coppythread.start ();
Unzipthread.start ();

Long end = System.currenttimemillis ();
Responding to user requests
System.out.println ("Congratulations, file upload successful, time-consuming:" + (End-begin) + "milliseconds");
}
}


Class UploadFile {
File Upload
Public File Uploadfilemethod () {
File File = new file ("FilePath");
System.out.println ("File upload complete");
return file;
}
}


Class Coppyfile implements Runnable {
private file file;

Public coppyfile (file file) {
This.file = file;
}

@Override
public void Run () {
Coppyfilemethod (file);
}

File copy
public void Coppyfilemethod (file file) {
Sleep 15 seconds
try {
Thread.Sleep (15000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
System.out.println ("File copy Complete");
}
}


Class Unzipfile implements Runnable {
private file file;

Public unzipfile (file file) {
This.file = file;
}

@Override
public void Run () {
Unzipfilemethod (file);

}

File decompression
public void Unzipfilemethod (file file) {
Sleep 10 seconds
try {
Thread.Sleep (10000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("File decompression complete");
}
}

Java multi-threaded upload Extract files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.