62,614
社区成员
发帖
与我相关
我的任务
分享
package com.wanju.project001.zonghe.test;
import java.util.ArrayList;
import java.util.List;
public class TestThread {
public static List aListSetTest=new ArrayList();
public static void main(String[] args) {
TestThread testThread = new TestThread();
testThread.testThread();
}
public void testThread()
{
aListSetTest.add("eeeeee");
new Thread(new T()).start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(new T()).start();
}
}
class T implements Runnable{
@Override
public void run() {
synchronized (TestThread.aListSetTest) {
test(this);
}
}
public void test(Runnable t)
{
while(true)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("t"+t.toString());
}
}
}
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
tcom.wanju.project001.zonghe.test.T@1cc2ea3f
package com.wanju.project001.zonghe.test;
import java.util.HashMap;
public class CacheObject {
OCache oCache = new OCache();
public static void main(String[] args) {
CacheObject test = new CacheObject();
test.init();
}
public void init()
{
oCache.setTimeYouxiao(3);//3s
getStudentById(1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getStudentById(1);
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
getStudentById(1);
}
public Object getStudentById(int id) {
Object result = oCache.getCacheObjectByKey(id+"");
if(null ==result){
StudentPojo student= new StudentPojo();
student.setId(1);
student.setName("zhangsan");
oCache.addObject(1+"", student);
return student;
}
return result;
}
}
class OCache extends ICache {
public synchronized HashMap getMap() {
return map;
}
public synchronized void addObject(String key, Object object) {
System.out.println("添加了缓存,因为缓存失效或者第一次添加对象");
put(key, object);
}
public synchronized Object getCacheObjectByKey(String key) {
return get(key);
}
}
abstract class ICache {
protected long timeYouxiao;
protected boolean isUseing;
protected HashMap<String, CachePack> map = new HashMap<String, CachePack>();
public void put(String key, Object object) {
map.put(key,
new CachePack(System.currentTimeMillis(), System
.currentTimeMillis() + timeYouxiao * 1000, key, object));
}
public Object get(String key) {
if(map.get(key)!=null && isShiXiao(key))
{
System.out.println("使用了缓存");
}
return map.get(key)==null || !isShiXiao(key)?null:(map.get(key).getObject());
}
public boolean isShiXiao(String key)
{
if(map.get(key).getCurrentTime()-System.currentTimeMillis()>0){
return true;
}
return false;
}
public long getTimeYouxiao() {
return timeYouxiao;
}
public void setTimeYouxiao(long timeYouxiao) {
this.timeYouxiao = timeYouxiao;
}
public boolean isUseing() {
return isUseing;
}
public void setUseing(boolean isUseing) {
this.isUseing = isUseing;
}
public HashMap<String, CachePack> getMap() {
return map;
}
public void setMap(HashMap<String, CachePack> map) {
this.map = map;
}
}
class CachePack {
private long savingTime;
private long currentTime;
private String key;
private Object object;
public CachePack() {
}
public CachePack(long savingTime, long currentTime, String key,
Object object) {
this.savingTime = savingTime;
this.currentTime = currentTime;
this.key = key;
this.object = object;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getObject() {
return object;
}
public void setObject(Object object) {
this.object = object;
}
public long getSavingTime() {
return savingTime;
}
public void setSavingTime(long savingTime) {
this.savingTime = savingTime;
}
public long getCurrentTime() {
return currentTime;
}
public void setCurrentTime(long currentTime) {
this.currentTime = currentTime;
}
}
class StudentPojo {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "StudentPojo [id=" + id + ", name=" + name + "]";
}
}