// Refer to Oreilly Net Work Programming For most programs, threads have their own overhead, starting threads and threads to exit cleaning work to consume a virtual machine quite amazing workload, especially a program generated a thousand threads. It is often happening for medium and medium-capacity web servers. Even if the thread can end quickly, she will also load the useless compilation module or virtual sequence. By re-use thread, you can improve the utilization of resources, and too much thread waste resources can be avoided. Once the thread exits, you can not start the thread again, but you can design the thread, so that the thread does not exit after the thread is completed, and all tasks you need to complete are placed in a queue or other data structure, and let each thread After completing the previous task, get the next task from the queue. The easiest way to implement the thread library is to use a fixed number of threads when the library is established. When the library is empty, each thread waits on the library. When the task is added to the library, all such as a thread is notified. When a thread completes the task assigned to it, the thread returns to the library and obtains a new task. If there is no new task, the thread waits, knowing a new task is added to the library.
Example: Complicates each file under the current directory into a Gzip format using GzipOutputStream.
GZipThread class import java.util *;. Import java.util.List; import java.io.File; import java.io.InputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.OutputStream Import java.io.fileoutputStream; import java.util.zip.gzipoutputstream; import java.io.bufferedoutputstream; import java.io ioException;
/ ** This class inherits Thread used to compress the file. He takes out the task from a collection until all tasks have completed * / public class gzipthread extends thread {/ ** * from Pool to remove tasks; * / private list pool; / * * * Statistics how many files have been compressed * / private static int filescompressed = 0; public gzipthread (list pool) {this.pool = pool;}
/ ** * This method must be synchronized, there is a possible different thread while FileCompRESSED * per compressed file call once this method * / private static synchronized void incrementFilesCompRESSED () {FilesCompRESSED ;}
Public void Run () {while (filescompressed! = gzipallfiles.getnumberoffilestobefiles.getnumberoffilestobeCompRESSED ()) {file input = null; synchronized (pool) {/ ** * There are three cases when there is no task in Collection * ● All tasks have been completed * ● When the program starts, the collection is empty * ● Not all files are added in the Collection, but the tasks added in the Collection have completed * / while (file.Issed == GzipAllFiles. GetNumberoffileStobeCompressed ()) {// All tasks have been completed;} try {pool.wait (); // Wait on this object} Catch (InterruptedException EX) {}} infut = (file) pool.remove Pool.size () - 1); // This time is not empty. // Get a new task} if (! INPUT.GETNAME (). Endswith (". Gz")) {// Do not compress the compressed file try {INPUTSTREAM IN = New FileInputStream (Input); in = New BufferedInputStream IN); file output = new file (Input.getParent (), input.getname () ".gz"); if (! output.exists ()) {// 文 文 不 存, start compression OutputStream Out = New fileOutputStream (OUTPUT); OUT = New GzipOutputStream (OUT); OUT = New BufferedoutputStream (OUT); INT B; While ((b = in.read ())! = -1) {Out.write (b);} Out.flush ();
Out.close (); inc.close ();} // complete compression process} catch (ioException e) {}} // End If} // End While, all tasks have been completed} / / End RUN method
}
Gzipallfiles class import java.util.vector; import java.io.file; public class gzipallfiles {public static void main (string [] args) {/ ** * All tasks in Vector * / Vector pool = new vector () ; / ** * Set the number of thread groups to four, remove the task from the vector and run * / gzipthread [] threads = new gzipthread [thread_count]; for (int i = 0; i Public final static int thread_count = 4; private static int fileestobecompressed = -1; }