Non-multithreaded package test;import Java.io.*;import java.security.digestinputstream;import java.security.messagedigest;/* * Using Digestinputstream to complete the message digest calculation, * A read method for this digest input stream is called first, followed by an digest method on the associated message digest. * This case contains two programs, one of which is multi-threaded and is compared with each other. */public class DigestThread2 {private static File input;private static long beginTime;p ublic static void Main (string[] Ar GS) {beginTime = System.currenttimemillis (); for (int i = 0; i < args.length; i++) {input = new File (args[i]); try {Filei Nputstream in = new FileInputStream (input); MessageDigest sha = messagedigest.getinstance ("Sha");D igestinputstream din = new Digestinputstream (in, SHA); int B;while ((b = Din.read ())! =-1);d in.close (); byte[] Digest = Sha.digest ();//using a character buffer cache StringBuffer res = new StringBuffer (input.t Ostring ()); Res.append (":"); for (int j = 0; J < Digest.length; J + +) {res.append (Digest[j] + "");} Long endTime = System.currenttimemillis (); System.out.println (Thread.CurrentThread (). GetName () + ":" +res+ "time consuming" + (Endtime-begintime)/1000+ "seconds");} catch (ExCeption e) {e.printstacktrace ();}}}}
Multithreading
Package Test;import Java.io.*;import Java.security.digestinputstream;import java.security.messagedigest;/* * Using Digestinputstream to complete the message digest calculation, * A read method for this digest input stream is called first, followed by an digest method on the associated message digest. * This case contains two programs, one of which is multi-threaded and is compared with each other. */public class Digestthread extends thread{private static long beginTime;p rivate file input;public digestthread (file Inpu T) {this.input=input;} ----------overwrite the Run () method----------public void Run () {try {fileinputstream in=new fileinputstream (input); MessageDigest sha=messagedigest.getinstance ("Sha");D Igestinputstream din=new digestinputstream (in, SHA); int B;while ((B=din.read ())!=-1);//constantly reading din.close (); byte[] Digest=sha.digest ();//using a character buffer cache StringBuffer Res=new StringBuffer ( Input.tostring ()); Res.append (":"); for (int i=0;i<digest.length;i++) {res.append (digest[i]+ "");} Long endTime = System.currenttimemillis (); System.out.println (CurrentThread (). GetName () + ":" +res+ "time consuming" + (Endtime-begintime)/1000+ "seconds");} catch (Exception e) {e.printstacktrace ();}} public static void Main (string[] args) {BeginTime = System.currenttimemillis (); for (int i=0;i<args.length;i++) {file F=new file (Args[i]); Thread T=new digestthread (f); T.setname ("Thread" + (i+1)); T.start ();} System.out.println ("Main () End!" ");}}
Note Set parameters before starting the program F:/f1.txt f:/f2-4.txt f:/f3.txt f:/f4.txt f:/f5.txt f:/f6.txt f:/f7.txt
It is advisable to read a single file around 10M, use multi-threading in frequent IO programs, and take advantage of CPU idle time.
A case of comparing multi-threaded (parallel) and non-multithreaded tasks that require the overhead of doing the same task (I/O frequent)