之前老师让我们用循环链表实现一个资源池的调用和释放,然后自己对链表有进行了一些了解:
链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的。每一个链表都包含多个节点,节点又包含两个部分,一个是数据域(储存节点含有的信息),一个是引用域(储存下一个节点或者上一个节点的地址)。所以链表是一个指向一个形成的一条链,如果你想让他们形成环状,只需要把首尾相连,如果是双向链表那么就需要两个引用域,一个指向前面,一个指向后面。
但是这里使用链表获取数据麻烦,需要遍历查找,比数组慢,但是方便借出和归还(插入、删除),所以使用链表要视情况而定!
下面是实现代码
package day1_23;
/**
* @author : [17151]
* @version : [v1.0]
* @description : [池里的对象类]
* @createTime : [2022/1/23 17:20]
* @updateUser : [17151]
* @updateTime : [2022/1/23 17:20]
* @updateRemark : [说明本次修改内容]
*
*/
public class PoolNode<T> {
private String name;//对象名
private int key;//对象身份
private PoolNode nextPoolNode;//当前对象指向的下一个对象
private int num;//被调用的次数
private int state;//当前被调用的状态,1:被使用 0:为空闲
public PoolNode(){
}
public PoolNode(String name, int key, PoolNode nextPoolNode, int num,int state) {
this.name = name;
this.key = key;
this.nextPoolNode = nextPoolNode;
this.num = num;
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public PoolNode getNextPoolNode() {
return nextPoolNode;
}
public void setNextPoolNode(PoolNode nextPoolNode) {
this.nextPoolNode = nextPoolNode;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
/*@Override
public String toString() {
return "PoolNode{" +
"name='" + name + '\'' +
", key=" + key +
", nextPoolNode=" + nextPoolNode +
", num=" + num +
", state=" + state +
'}';
}*/
}
package day1_23;
/**
* @author : [17151]
* @version : [v1.0]
* @description : []
* @createTime : [2022/1/23 16:42]
* @updateUser : [17151]
* @updateTime : [2022/1/23 16:42]
* @updateRemark : [说明本次修改内容]
*/
/**
* 一个元素只能同时被借出一次
* 使用链表实现
* 尽可能池内元素平均使用
*/
public class Pool {
//头结点
private PoolNode heard;
//池 里面存放十个对象
public Pool() {
heard = new PoolNode("对象0",1,null,0,0);
heard.setNextPoolNode(heard);
PoolNode temp = heard;
for (int i = 1; i < 10; i++) {
PoolNode poolNode = new PoolNode("对象"+i,1,null,0,0);
if(temp.getNextPoolNode().equals(heard)){
heard.setNextPoolNode(poolNode);
}else {
while (!temp.getNextPoolNode().equals(heard)) {
temp = temp.getNextPoolNode();
}
temp.setNextPoolNode(poolNode);
}
poolNode.setNextPoolNode(heard);
}
}
public static void main(String[] args) throws InterruptedException {
Pool pool = new Pool();
PoolNode node1 = pool.borrow();
pool.borrow();
pool.borrow();
pool.borrow();
pool.borrow();
pool.borrow();
PoolNode node2 = pool.borrow();
pool.borrow();
pool.borrow();
pool.borrow();
pool.borrow();
pool.giveBack(node1);
pool.giveBack(node2);
pool.borrow();
pool.borrow();
}
/**
* 给一个空闲对象出去。如果没有空闲的。返回null
*/
public PoolNode borrow(){
PoolNode temp = heard;
while (!temp.getNextPoolNode().equals(heard)){
if(temp.getState()!=1){
if(temp.getNum()>temp.getNextPoolNode().getNum()){
temp = temp.getNextPoolNode();
}
temp.setNum(temp.getNum()+1);
temp.setState(1);
System.out.println(temp.getName()+"被借出!");
return temp;
}
temp = temp.getNextPoolNode();
}
System.out.println("没有对象可以被借出!");
return null;
}
/**
* 还借的。正常返回true.
* 不是池内借出的。返回false
*/
public boolean giveBack(PoolNode obj) {
if (obj.getKey() == 1) {
obj.setState(0);
System.out.println("还回一个对象:" + obj.getName());
return true;
} else {
System.out.println("对象不对,无法返还");
}
return false;
}
}