public static synchronized void runInSecurity(SecurityManager m,final Runnable task,int millisecondsTimeOut){
    final SecurityManager old=System.getSecurityManager();
    final SecurityManager delegate=m;
    final boolean[]allows={false};
    System.setSecurityManager(new SecurityManager() {
    	public void checkPermission(Permission p) {
    		if (p instanceof RuntimePermission && "setSecurityManager".equals(p.getName()) && allows[0]){
    			return;
    		}
    		else delegate.checkPermission(p);
    	}
    });
    FutureTask<Object> control=new FutureTask<Object>(new Callable<Object>(){
      public Object call() throws Exception {task.run();return null;}}
    );
    Thread t=new Thread(control);
    t.start();
    try { control.get(millisecondsTimeOut, TimeUnit.MILLISECONDS);}
    catch (TimeoutException ex) {
      throw new Error("The run time of your code was over the fixed time limit");}
    catch (InterruptedException ex) {
      throw new Error("We experienced some tecnical difficulties, try to submit your code again.");}
    catch (ExecutionException ex) {
      throw new Error(ex.getCause());}
    finally{
      allows[0]=true;
      System.setSecurityManager(old);
      control.cancel(true);
      //log.finer("Stopping thread %s".format(t.getName()));
      t.stop();
      }
    }