Skip to content

Commit

Permalink
Merge pull request dracome#29 from lingchen1dian21fen/master
Browse files Browse the repository at this point in the history
JVM第一周作业
  • Loading branch information
zhijiaxinyu authored Mar 31, 2017
2 parents fd411cd + c9152aa commit 0dd2be9
Show file tree
Hide file tree
Showing 18 changed files with 680 additions and 203 deletions.
135 changes: 135 additions & 0 deletions group10/904627477/src/com/coding/basic/LRUPageFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.coding.basic;

/**
* 用双向链表实现LRU算法
* @author liuxin
*
*/
public class LRUPageFrame {

private static class Node {

Node prev;
Node next;
int pageNum;

Node() {
}
}

private int capacity;


private Node first;// 链表头
private Node last;// 链表尾


public LRUPageFrame(int capacity) {

this.capacity = capacity;

}

/**
* 获取缓存中对象
*
* @param key
* @return
*/
public void access(int pageNum) {
Node node = findPageNumNode(pageNum);
if(node==null){
if(size()>=capacity){
removeLast();
}
push(pageNum);
}else{
moveToFirst(node);
}
}

public Node findPageNumNode(int pageNum) {
Node node = first;
while(node!=null){
if(node.pageNum==pageNum){
break;
}else{
node = node.next;
}
}
return node;
}

public void moveToFirst(Node node) {
if(first==null||node==null||first.pageNum==node.pageNum){
return;
}
if(node.pageNum == last.pageNum){
node.prev.next = null;
last = node.prev;
}else{
Node tPrev = node.prev;
Node tNext = node.next;
tPrev.next = tNext;
tNext.prev = tPrev;
}
node.prev = null;
node.next = first;
first = node;
}

public void push(int pageNum) {
if(size()==0){
first = new Node();
first.pageNum = pageNum;
last = first;
return ;
}
Node node;
node = new Node();
node.pageNum = pageNum;
node.next = first;
first.prev = node;
first = node;
}

public void removeLast() {
if(last==null){
return ;
}
if(size()==1){
first = null;
last = null;
return ;
}
Node temp = last.prev;
last.prev = null;
last = temp;
last.next = null;
}

public int size() {
int size = 0;
Node temp = first;
while (temp!=null) {
size++;
temp = temp.next;
}
return size;
}

public String toString(){
StringBuilder buffer = new StringBuilder();
Node node = first;
while(node != null){
buffer.append(node.pageNum);

node = node.next;
if(node != null){
buffer.append(",");
}
}
return buffer.toString();
}

}
64 changes: 32 additions & 32 deletions group10/904627477/src/com/coding/download/CreateThread.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.coding.download;


import com.coding.download.api.Resource;

public class CreateThread extends Thread {

private Resource resource;
private int length;

public CreateThread(Resource resource,int length){
this.resource = resource;
this.length = length;
}

@Override
public void run() {
int startPos = 0;
while(true){
//System.out.println(startPos);
if(startPos>=length){
resource.setFlag(true);
break;
}else{
startPos = resource.increace();
}
}
}



}
package com.coding.download;


import com.coding.download.api.Resource;

public class CreateThread extends Thread {

private Resource resource;
private int length;

public CreateThread(Resource resource,int length){
this.resource = resource;
this.length = length;
}

@Override
public void run() {
int startPos = 0;
while(true){
//System.out.println(startPos);
if(startPos>=length){
resource.setFlag(true);
break;
}else{
startPos = resource.increace();
}
}
}



}
20 changes: 18 additions & 2 deletions group10/904627477/src/com/coding/download/DownloadThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.coding.download.api.Connection;
import com.coding.util.IOUtils;
Expand All @@ -13,9 +14,17 @@ public class DownloadThread extends Thread {
int startPos;
int endPos;
private File file;
private CyclicBarrier barrier;

public DownloadThread(Connection conn, int startPos, int endPos,File file,CyclicBarrier barrier) {
this.barrier = barrier;
this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
this.file = file;
}

public DownloadThread(Connection conn, int startPos, int endPos,File file) {

this.conn = conn;
this.startPos = startPos;
this.endPos = endPos;
Expand All @@ -29,8 +38,15 @@ public void run() {
if(buff!=null&&buff.length!=0){
IOUtils.writeFile(file, startPos, buff);
}
if(barrier!=null){ //修改后代码
barrier.await();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
}
}
50 changes: 24 additions & 26 deletions group10/904627477/src/com/coding/download/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package com.coding.download;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CyclicBarrier;

import com.coding.download.api.Connection;
import com.coding.download.api.ConnectionException;
import com.coding.download.api.ConnectionManager;
import com.coding.download.api.DownloadListener;
import com.coding.download.api.Resource;
import com.coding.util.IOUtils;


Expand All @@ -26,6 +21,7 @@ public class FileDownloader {
ConnectionManager cm;

private static String localFile = "c:/test/test.jpg";
private final static int MAX_THREAD_NUM = 3;


public FileDownloader(String _url) {
Expand All @@ -47,14 +43,12 @@ public void execute(){
// 4. 所有的线程都下载完成以后, 需要调用listener的notifiedFinished方法

// 下面的代码是示例代码, 也就是说只有一个线程, 你需要改造成多线程的。
/**/
/* my
try {
Connection conn = cm.open(url);
int length = conn.getContentLength();
File file = new File(localFile);
if(!file.exists()){
IOUtils.createFile(length, localFile);
}
IOUtils.createFile(length, localFile);
Resource res = new Resource(url,file);
Thread c = new CreateThread(res,length);
Thread r = new RemoveThread(res,listener);
Expand All @@ -63,26 +57,30 @@ public void execute(){
} catch (ConnectionException e) {
e.printStackTrace();
}
/*Connection conn = null;
*/
try {
conn = cm.open(this.url);
int length = conn.getContentLength();
CyclicBarrier barrier = new CyclicBarrier(MAX_THREAD_NUM, new Runnable() {
@Override
public void run() {
listener.notifyFinished();
}
});
Connection conn = cm.open(url);
int length = conn.getContentLength();
IOUtils.createFile(length, localFile);
File file = new File(localFile);
if(!file.exists()){
IOUtils.createFile(length, localFile);
int size = length/MAX_THREAD_NUM;
int last = length%MAX_THREAD_NUM;
for(int i=0;i<MAX_THREAD_NUM;i++){
int startPos = i*size;
int endPos = (i+1)*size-1;
endPos = i==(MAX_THREAD_NUM-1)?(endPos+last):endPos;
DownloadThread dt = new DownloadThread(cm.open(url),startPos ,endPos , file, barrier);
dt.start();
}
new DownloadThread(cm.open(this.url),0,length-1,file).start();
} catch (ConnectionException e) {
} catch (ConnectionException e) {
e.printStackTrace();
}finally{
if(conn != null){
conn.close();
}
}*/
}
}

public void setListener(DownloadListener listener) {
Expand Down
46 changes: 23 additions & 23 deletions group10/904627477/src/com/coding/download/api/Connection.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package com.coding.download.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();
}
package com.coding.download.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();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.coding.download.api;

@SuppressWarnings("serial")
public class ConnectionException extends Exception {

public ConnectionException(Exception e){
super(e);
}

}
Loading

0 comments on commit 0dd2be9

Please sign in to comment.