标签:
守护线程只是个概念问题,一句话可以总结(不知道总结的对不对^_^);
当所有用户线程都结束的时候,守护线程也就结束了,当有用户线程存在的时候,守护线程就是一个普通线程。
main线程不可以设置成守护线程,应为只有在线程调用start方法前,才可以设置线程为守护线程,main线程是jvm创建的
多线程以及线程池的问题
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); public 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(); down.down(); // down.executorService.isTerminated() down.executorService.shutdown(); while (true) { if (down.executorService.isTerminated()) { System.out.println("所有的已经下载完毕"); break; } try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } class DownImg implements Runnable { int i; String imgUrl; URL url; int len; byte[] bytes = new byte[1024]; public DownImg(String imgUrl, int i) { this.i = i; this.imgUrl = imgUrl; try { url = new URL(imgUrl); } catch (MalformedURLException e) { e.printStackTrace(); } } @Override public 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("第" + i + "张下载完毕"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
down.executorService.shutdown();这个方法,是在线程池中的线程只执行一次任务;
如果没有调用,线程池的线程执行完一次任务后,会一直处于等待状态
标签:
原文地址:http://www.cnblogs.com/nongmei/p/4666864.html