问两道笔试题,关于缓存和线程的

江南编程浪子 2014-08-05 08:45:01
加精

请帮忙看看以下的2道Java笔试题

1.Implement an in-memory cache. What we know about the use case:

- The TTL of the items is fairly short (~10 seconds)
- The system has plenty of memory
- The keys are widely distributed across the keyspace
- Each item may or may not be accessed during the TTL
- Each item might be accessed multiple times during the TTL

1. 实现一个内存里的缓存,已知用例有:

-缓存内的对象存活时间很短,大约10秒左右
-系统有非常多的内存(足够用)
-缓存内存放对象的key是广泛分布于整个keyspace的
-每个对象在生命周期里有可能被访问也可能不被访问
-每个对象可能被访问多次

2.Create a simple framework where work items can be submitted. Each work item is an instance of a class and the definition of “parallelism”, which controls how many threads are created to execute the work item.
The framework makes sure that the number of threads executing the work item should remain the same until the threads finished executing the work item, eg.: if the work item dies, the framework should restart it.
There is no need to cater for timeouts.

Sample interfaces for the framework:

创建一个简单的框架可以提交执行任务。每个执行任务是一个对象实例,并且定义了并行执行的数量,就是说,每个执行任务定义了有多少个线程去执行这些任务。
这个框架要确保执行这些任务的线程数量在执行过程中是一直保持不变的,比如说,执行过程有有任务死掉了,框架要去重启这个线程。
不需要考虑超时的问题。

下面是框架的一些接口:

public interface WorkItemExecutor
{
void executeWorkItem(WorkItem w, int parallelism);
}

public interface WorkItemCompletionCallback
{
void complete();
}

public interface WorkItem
{
void execute(WorkItemCompletionCallback callback);
}
...全文
6121 48 打赏 收藏 转发到动态 举报
写回复
用AI写文章
48 条回复
切换为时间正序
请发表友善的回复…
发表回复
江南编程浪子 2014-10-07
  • 打赏
  • 举报
回复
although there is not satisfied answer here, finally, i got another job which has a good pay. Thank you all.
老李家的小二 2014-08-11
  • 打赏
  • 举报
回复
很是有难度啊
  • 打赏
  • 举报
回复
laoer_2002 2014-08-08
  • 打赏
  • 举报
回复
不懂,学习
beyondcj 2014-08-07
  • 打赏
  • 举报
回复
sinat_19073365 2014-08-07
  • 打赏
  • 举报
回复
不太懂,学习了
hugh_z 2014-08-07
  • 打赏
  • 举报
回复
learning
可爱的安妮 2014-08-07
  • 打赏
  • 举报
回复
学习了~ java 刚入门
GW786228836 2014-08-07
  • 打赏
  • 举报
回复
致知Fighting 2014-08-07
  • 打赏
  • 举报
回复
引用 24 楼 wangbo1118 的回复:
[quote=引用 17 楼 ygycomon 的回复:] 第一个理解不太对。你这样搞就不是缓存了,性能很差的。缓存就是内存操作。关于数据持久化,可以有另一个策略,你可以参考redis的两种持久化策略,但是绝对不是同步的先处理内存在处理持久化,这么搞缓存就没有意义了。 第二个理解是可行的。但是操作是有代价的,如果你get的时候再去删,有些key如果不被get就永远占着内存不释放了。 不过这些细节都无所谓,毕竟面试笔试都是吹水,随便忽悠都可以
其实第一个我的意思是为数据库做的缓存,解释的是怎么协作的 我在想,我们平时缓存一般存在多久都是不知道的,就是缓存容量满了就不存了,或者直到被更新和删除 一般缓存的容量都是有限的,所以一般的策略就是存放最近使用过的内容,如果最近使用过的内容被命中,那就时间被更新,最先被踢出缓存的就是缓存里最久没有被用到的那个 而这里引入了的这个ttl的概念就是time to live,生存周期的概念,似乎我只在DNS域名服务器里碰到过 就是DNS查到域名的ip以后,同时会得到一个ttl,然后将这个ip和域名的对应关系缓存在本地(一个人一般一天会访问同一个网站很多次),当ttl指定的时间失效以后,机器就会再次去查询了 所以我觉得这里既然引入了ttl,应该也是类似的想法吧,就是加到缓存里的时候就同时有一个过期时间传进来,然后每次查询的时候就判断这个值是否过期,不过期就返回给他,过期就要重新去取了 你的理解呢? 我现在碰到的例子,只有DNS服务器是缓存有 [/quote] ttl很多地方都有啊,tcp里就有ttl,redis里也有ttl,缓存怎么可以没有过期时间呢,本地缓存可能没有太多这种概念,但凡是缓存服务器都会有ttl这种概念。实现的机制和优劣分析我上面已经提到过了,你可以再看看。 缓存的实现思路,参考redis
qq_19058337 2014-08-07
  • 打赏
  • 举报
回复
呵呵不太懂啊
Std_Wang 2014-08-07
  • 打赏
  • 举报
回复
刚开始学java,看不太懂,先收藏了
cattpon 2014-08-07
  • 打赏
  • 举报
回复
很经典的题目~
doer_ljy 2014-08-07
  • 打赏
  • 举报
回复
有点意思,其实跟Java的垃圾回收策略很相似,不过没有这么复杂罢了。
alexander0729 2014-08-07
  • 打赏
  • 举报
回复
这两道题目太宽了, 第一题: 缓存这个题目非常的大, 从建立, 搜索, 使用, 回收, 等等, 有太多的花样可以玩, 单单就缓存的搜索, 如果算法不是很好的话, 最好不要乱写。 第二题: 任务调度管理, 可以说整个OS就是一个任务调度管理器, 这里面的东西也太多了, 面试的时间内, 说都说不完, 怎么可能写完呢, 第一个也是, 说都说的不完。 所以如果在面试的时候, 如果遇见了上面的2个问题, 就看运气了, 如果面试官跟你挺聊得来, 那么你只要别乱忽悠, 就让你过了, 如果你运气不好, 那肯定过不了。
静山晚风 2014-08-06
  • 打赏
  • 举报
回复
多线程对应锁住对象实例
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
静山晚风 2014-08-06
  • 打赏
  • 举报
回复
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 + "]";
}

}

菜鸟大明 2014-08-06
  • 打赏
  • 举报
回复
引用 10 楼 wangbo1118 的回复:
[quote=引用 2 楼 zhao9tian 的回复:] sorry,有个地方写错了 第40行的 for (int i=0;i<10;i++) { 应该换成 for (int parallelism=0;parallelism<10;i++) {
谢谢你的代码,我看看 for (int parallelism=0;parallelism<10;i++) 这行有问题吧,parallelism是入参,这里再声明不就重复了吗? 还有为什么使用Executors.newCachedThreadPool方法而不是Executors.newFixedThreadPool方法呢?[/quote] sorry,各种笔误 应该是这样: for (int i=0;i<parallelism;i++) 理解意思~理解意思。 缓存那个,我的理解不应该依赖第三方API应用吧,redis ehCache等都不应该是此次面试题的目的。 这两道题应该考的是你对JDK自身API的理解,个人认为哈。 缓存那个,今儿下班前,我要是有时间,我再写一段。
youbinghua 2014-08-06
  • 打赏
  • 举报
回复
好 太有用了 谢谢楼主
flaybingxia 2014-08-06
  • 打赏
  • 举报
回复
- -不看不知道,一看还是有点不懂
加载更多回复(21)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧