1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| import java.util.concurrent.Callable;
public class LeetCode { public static void main(String[] args) { Callable<Long> callable = new Task(123450000L); Thread thread = new Thread(new RunnableAdapter(callable)); thread.start(); } }
class RunnableAdapter implements Runnable { private Callable<?> callable;
RunnableAdapter(Callable<?> callable) { this.callable = callable; } @Override public void run() { try { callable.call(); } catch (Exception e) { throw new RuntimeException(e); } }
}
class Task implements Callable<Long> { private long num;
public Task(long num) { this.num = num; } @Override public Long call() throws Exception { long sum = 0; for (long i = 0; i < num; i++) { sum += i; } System.out.println("result:" + sum); return sum; }
}
|