I have created blank project and try do next code. Unfortunately I got problem:
Main activity does’t load while don’t finish my thread. If don’t use return value from thread all work normal.
unfortunately
final ExecutorService service;
final Future task;
service = Executors.newFixedThreadPool(1);
task = service.submit(new Foo());
try
{
final String str;
// waits the 10 seconds for the Callable.call to finish.
str = task.get();
Log.d("VSK",str);
}
catch(final InterruptedException ex)
{
ex.printStackTrace();
}
catch(final ExecutionException ex)
{
ex.printStackTrace();
}
service.shutdownNow();
}
class Foo
implements Callable<String>
{
public String call()
{
try
{
// sleep for 10 seconds
Thread.sleep(10 * 1000);
}
catch(final InterruptedException ex)
{
ex.printStackTrace();
}
return ("Hello, World!");
}
}
This line
str = task.get();blocks main thead while your callable being executed in other thread. Return result from your task via Handler or use AsyncTask.