Returning a value from Runnable

Use Callable<V> instead of using Runnable interface. Example: public static void main(String args[]) throws Exception { ExecutorService pool = Executors.newFixedThreadPool(3); Set<Future<Integer>> set = new HashSet<>(); for (String word : args) { Callable<Integer> callable = new WordLengthCallable(word); Future<Integer> future = pool.submit(callable); set.add(future); } int sum = 0; for (Future<Integer> future : set) { sum += future.get(); … Read more

Is there a way to make Runnable’s run() throw an exception?

You can use a Callable instead, submitting it to an ExecutorService and waiting for result with FutureTask.isDone() returned by the ExecutorService.submit(). When isDone() returns true you call FutureTask.get(). Now, if your Callable has thrown an Exception then FutureTask.get() wiill throw an Exception too and the original Exception you will be able to access using Exception.getCause().

Runnable with a parameter?

Well it’s been almost 9 years since I originally posted this and to be honest, Java has made a couple improvements since then. I’ll leave my original answer below, but there’s no need for people to do what is in it. 9 years ago, during code review I would have questioned why they did it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)