package com.github.miniyk2012.coding2017.coderising.download; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.github.miniyk2012.coding2017.coderising.download.api.ConnectionManager; import com.github.miniyk2012.coding2017.coderising.download.impl.ConnectionManagerImpl; public class FileDownloaderTest { boolean downloadFinished = false; private double time = 0; @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void testDownload() { String url = "http://inews.gtimg.com/newsapp_bt/0/1209438116/1000"; // String url = "https://www.baidu.com/img/bd_logo.png"; String filePath = "src/main/resources/downloads/test.png"; FileDownloader downloader = new FileDownloader(url, filePath); ConnectionManager cm = new ConnectionManagerImpl(); downloader.setConnectionManager(cm); downloader.setListener(() -> downloadFinished = true); downloader.execute(); // 等待多线程下载程序执行完毕 while (!downloadFinished) { try { System.out.println("还没有下载完成,休眠0.01秒"); time += 0.01; //休眠0.01秒 Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("下载完成!耗时"+time+"秒"); } }