forked from dracome/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 0
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 dracome#29 from lingchen1dian21fen/master
JVM第一周作业
- Loading branch information
Showing
18 changed files
with
680 additions
and
203 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
group10/904627477/src/com/coding/basic/LRUPageFrame.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,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
64
group10/904627477/src/com/coding/download/CreateThread.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 |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
} |
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
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
46 changes: 23 additions & 23 deletions
46
group10/904627477/src/com/coding/download/api/Connection.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 |
---|---|---|
@@ -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(); | ||
} |
5 changes: 5 additions & 0 deletions
5
group10/904627477/src/com/coding/download/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 |
---|---|---|
@@ -1,5 +1,10 @@ | ||
package com.coding.download.api; | ||
|
||
@SuppressWarnings("serial") | ||
public class ConnectionException extends Exception { | ||
|
||
public ConnectionException(Exception e){ | ||
super(e); | ||
} | ||
|
||
} |
Oops, something went wrong.