title: killing a java thread in test
body: <p>I'm writing a sort of tutorial about programming (it will be a Java repo on github) where users can clone the repo and write their own code inside empty methods to solve algorithmic problems. After they write their code, they can launch unit tests to check if their solution is correct and if it completes execution in less than a certain time (to assure they found the most efficient solution). 
  So my repo will contain a lot of classes with empty methods and all the non-empty unit tests to check the code the users will write.</p>
  
  <p>What I'm doing in the JUnit tests is something like that:</p>
  
  <pre><code>// Problem.solveProblem() can be a long running task
  Thread runner = new Thread(() -&gt; Problem.solveProblem(input)); 
  
  runner.start();
  
  try {
      Thread.currentThread().sleep(500);
  }
  catch (InterruptedException e) {
      e.printStackTrace();
  }
  
  if (runner.isAlive()) {
      fail("Your algorithm is taking too long.");
      runner.stop();
  }
  </code></pre>
  
  <p>Now, if a user writes a not optimized algorithm, the test fails correctly, but the <code>runner</code> thread will continue to run (and so will do the test thread) until it terminates, which can happen after minutes, though I call <code>running.stop()</code>. So I have tests that can last minutes instead of seconds like I'd like. </p>
  
  <p>I know how to gracefully kill a thread in Java, but in this case I don't want the users to take care of multithreading issues (like checking/updating shared variables): I just want them to write only the code to solve the problem.</p>
  
  <p>So my question is: is there a way to abruptly kill a thread in Java? If not, is there any other approach I could follow to accomplish my goal?</p>
  
  <p>Thanks,
  Andrea</p>