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

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

请帮忙看看以下的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);
}

------解决思路----------------------
楼主,第一道题我没看懂要求。
第二道题,我大概明白你的意思了,不过说实话,这两道面试题的难度真不是盖的。
第二题我大概做了一些,感觉挺有意思的,但也确实有一些难度,我自己也没有觉得很有把握。
上代码

package com.test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 菜鸟大明
 * Date: 14-8-5
 * Time: 下午9:24
 * To change this template use File 
------解决思路----------------------
 Settings 
------解决思路----------------------
 File Templates.
 */

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

interface WorkItemCompletionCallback
{
    void complete();
}

interface WorkItem
{
    void execute(WorkItemCompletionCallback callback);
}

public class MyWorkItemExecutor implements WorkItemExecutor{

    // 通过我们自己写的ExceptionThreadFactory线程工厂,构造线程池
    static final ExecutorService executorService = Executors.newCachedThreadPool(new ExceptionThreadFactory());

    @Override
    public void executeWorkItem(WorkItem w, int parallelism) {

        try {
            System.out.println("执行线程");
            for (int i=0;i<10;i++) {
                executorService.execute(new MyThread(i));
            }

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("捕捉异常");
        }
    }

    public void addThread(int i) {
        // 通过我们自己写的ExceptionThreadFactory线程工厂,构造线程池
        executorService.execute(new MyThread(i));
    }
}

/**
 * 测试类
 */
class Test{
    public static void main(String[] args) {
        MyWorkItemExecutor myWorkItemExecutor = new MyWorkItemExecutor();
        myWorkItemExecutor.executeWorkItem(new MyThread(),10);
    }
}

/**
 * 运行线程
 */
class MyThread implements Runnable,WorkItem{
    MyThread(){};