-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from starlight0405/master
thrid homework
- Loading branch information
Showing
13 changed files
with
840 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import api.Connection; | ||
|
||
import java.io.RandomAccessFile; | ||
import java.util.concurrent.CyclicBarrier; | ||
|
||
public class DownloadThread extends Thread { | ||
|
||
Connection conn; | ||
int startPos; | ||
int endPos; | ||
|
||
private CyclicBarrier barrier; | ||
|
||
public DownloadThread(Connection conn, int startPos, int endPos, CyclicBarrier barrier) { | ||
|
||
this.conn = conn; | ||
this.startPos = startPos; | ||
this.endPos = endPos; | ||
this.barrier = barrier; | ||
} | ||
|
||
public void run() { | ||
try { | ||
byte[] buffer = conn.read(startPos, endPos); | ||
RandomAccessFile raf = new RandomAccessFile("/Users/nvarchar/example.jpg", "rw"); | ||
raf.seek(startPos); | ||
raf.write(buffer); | ||
raf.close(); | ||
barrier.await(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import api.Connection; | ||
import api.ConnectionException; | ||
import api.ConnectionManager; | ||
import api.DownloadListener; | ||
|
||
import java.util.concurrent.CyclicBarrier; | ||
|
||
|
||
public class FileDownloader { | ||
|
||
String url; | ||
|
||
DownloadListener listener; | ||
|
||
ConnectionManager cm; | ||
|
||
private static final int THREAD_NUM = 10; | ||
|
||
|
||
public FileDownloader(String _url) { | ||
this.url = _url; | ||
|
||
} | ||
|
||
public void execute() { | ||
// (2) DownloadListener, 由于是多线程下载, 调用这个类的客户端不知道什么时候结束,所以你需要实现当所有 | ||
// 线程都执行完以后, 调用listener的notifiedFinished方法, 这样客户端就能收到通知。 | ||
CyclicBarrier barrier = new CyclicBarrier(THREAD_NUM, new Runnable() { | ||
@Override | ||
public void run() { | ||
listener.notifyFinished(); | ||
} | ||
}); | ||
Connection conn = null; | ||
try { | ||
//(1) ConnectionManager , 可以打开一个连接,通过Connection可以读取其中的一段(用startPos, endPos来指定) | ||
conn = cm.open(this.url); | ||
|
||
// 1. 需要调用ConnectionManager的open方法打开连接, 然后通过Connection.getContentLength方法获得文件的长度 | ||
int length = conn.getContentLength(); | ||
|
||
// 2. 至少启动3个线程下载, 注意每个线程需要先调用ConnectionManager的open方法 | ||
// 然后调用read方法, read方法中有读取文件的开始位置和结束位置的参数, 返回值是byte[]数组 | ||
int start = 0; | ||
int endPos = 0; | ||
for (int i = 0; i < THREAD_NUM; i++) { | ||
endPos = start + length / THREAD_NUM; | ||
System.out.println(start + "=====" + endPos); | ||
new DownloadThread(conn, start, endPos > (length - 1) ? length - 1 : endPos, barrier).start(); | ||
start = endPos + 1; | ||
} | ||
} catch (ConnectionException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
if (conn != null) { | ||
conn.close(); | ||
} | ||
} | ||
} | ||
|
||
public void setListener(DownloadListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
|
||
public void setConnectionManager(ConnectionManager ucm) { | ||
this.cm = ucm; | ||
} | ||
|
||
public DownloadListener getListener() { | ||
return this.listener; | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
group16/2816977791/thirdExercise/src/FileDownloaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import api.ConnectionManager; | ||
import api.DownloadListener; | ||
import impl.ConnectionManagerImpl; | ||
|
||
public class FileDownloaderTest { | ||
boolean downloadFinished = false; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testDownload() { | ||
|
||
String url = "https://cdn.pixabay.com/photo/2017/03/31/15/34/sunset-2191645_1280.jpg"; | ||
|
||
FileDownloader downloader = new FileDownloader(url); | ||
|
||
|
||
ConnectionManager cm = new ConnectionManagerImpl(); | ||
downloader.setConnectionManager(cm); | ||
|
||
downloader.setListener(new DownloadListener() { | ||
@Override | ||
public void notifyFinished() { | ||
downloadFinished = true; | ||
} | ||
|
||
}); | ||
|
||
|
||
downloader.execute(); | ||
|
||
// 等待多线程下载程序执行完毕 | ||
while (!downloadFinished) { | ||
try { | ||
System.out.println("还没有下载完成,休眠五秒"); | ||
//休眠5秒 | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
System.out.println("下载完成!"); | ||
|
||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package api; | ||
|
||
import java.io.IOException; | ||
|
||
public interface Connection { | ||
/** | ||
* 给定开始和结束位置, 读取数据, 返回值是字节数组 | ||
* @param startPos 开始位置, 从0开始 | ||
* @param endPos 结束位置 | ||
* @return | ||
*/ | ||
public byte[] read(int startPos,int endPos) throws IOException; | ||
/** | ||
* 得到数据内容的长度 | ||
* @return | ||
*/ | ||
public int getContentLength(); | ||
|
||
/** | ||
* 关闭连接 | ||
*/ | ||
public void close(); | ||
} |
5 changes: 5 additions & 0 deletions
5
group16/2816977791/thirdExercise/src/api/ConnectionException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package api; | ||
|
||
public class ConnectionException extends Exception { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
group16/2816977791/thirdExercise/src/api/ConnectionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package api; | ||
|
||
public interface ConnectionManager { | ||
/** | ||
* 给定一个url , 打开一个连接 | ||
* @param url | ||
* @return | ||
*/ | ||
public Connection open(String url) throws ConnectionException; | ||
} |
5 changes: 5 additions & 0 deletions
5
group16/2816977791/thirdExercise/src/api/DownloadListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package api; | ||
|
||
public interface DownloadListener { | ||
public void notifyFinished(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package basic; | ||
|
||
/** | ||
* @author nvarchar | ||
* date 2017/3/27 | ||
*/ | ||
public interface Iterator { | ||
boolean hasNext(); | ||
|
||
Object next(); | ||
} |
Oops, something went wrong.