The daemon thread is just a conceptual problem, a sentence can be summed up (do not know the summary of the right ^_^);
When all user threads are finished, the daemon thread is finished, and when there is a user thread, the daemon is a normal thread.
The main thread cannot be set as a daemon thread, it should be set to a daemon thread only before the thread calls the Start method, and the main thread is created by the JVM
Multithreading and thread pool issues
Import Java.io.datainputstream;import java.io.file;import java.io.fileoutputstream;import java.io.IOException; Import Java.net.malformedurlexception;import java.net.url;import Java.util.arraylist;import Java.util.List;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class Down {ExecutorService Executorservice = Executors.newfixedthreadpool (3);p ublic void Down () {Boolean a = new File ("c:/123/"). mkdir (); list<string> urls = new arraylist<string> (); Urls.add ("http://img1.cache.netease.com/catchpic/6/66/ 66f01df5fa318f96fe695d4a1ca1a698.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" Http://img1.cache.netease. com/catchpic/c/c5/c5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/ C5/c5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); Urls.add (" http://img1.cache.netease.com/catchpic/C/C5/ C5625e5d94afbbf73dbb8c88d762a634.jpg "); for (int i = 0; i < urls.size (); i++) {Executorservice.execute (New downimg ( Urls.get (i), i));}} public static void Main (string[] args) {down-down = new-down ();d own.down ();//down.executorService.isTerminated () Down.executorService.shutdown (); while (true) {if (down.executorService.isTerminated ()) {System.out.println (" All of them have been downloaded "); try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Class Downimg ImplementsRunnable {int i; String Imgurl; URL url;int len;byte[] bytes = new byte[1024];p ublic downimg (String imgurl, int i) {this.i = I;this.imgurl = imgurl;try {u RL = new URL (Imgurl);} catch (Malformedurlexception e) {e.printstacktrace ();}} @Overridepublic void Run () {try {datainputstream datainputstream = new DataInputStream (Url.openstream ()); FileOutputStream FileOutputStream = new FileOutputStream ("c:/123/" + i + ". jpg"); while (len = datainputstream.read (bytes )) > 0) {fileoutputstream.write (bytes, 0, len);} Datainputstream.close (); Fileoutputstream.close (); System.out.println ("First" + i + "Download Complete");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Down.executorService.shutdown (); This method is that threads in the thread pool perform only one task at a time;
If there is no call, the thread pool's threads are waiting until the task is completed
multithreaded thread pool Daemon threads