multithreaded thread pool Daemon threads

Source: Internet
Author: User

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

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.