This functionality is achieved through the use of Threads pools, available in Java, from version 1.5 on Executors Framework
Instead of creating a Thread
, you create a Pool with a Thread, and submits to it the tasks that you want this Thread to perform. For this your routine should implement a Callable<T>
. This is a simple interface, which has a method call
, the equivalent of run
that you would implement into a common Thread, only with a subtle and powerful difference: It allows you to return a value of a kind.
V call() throws Exception;
Let’s go to the code:
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) throws Exception{
String resultado = "";
//Criando um pool de Threads com uma Thread,
//equivalente ao sua Thread
ExecutorService executor = Executors.newSingleThreadExecutor();
//ao inves de chamarmos o metodo Thread.start(), chamamos:
//executor.submit -> o que já permitirá que você obtenha um envólucro para
//o resultado
Future<String> futureResult = executor.submit(new Callable<String>() {
//Note como este não é um Runnable, e sim um Callable("primo" do Runnable)
@Override
public String call() throws Exception {
String retorno = "";
//processando
//sua lógica
// e assim por diante
retorno = "abcde";
return retorno;
}
});
//Obtendo um resultado da execucão da Thread
resultado = futureResult.get();
System.out.println(resultado);
//lembrar de chamar o shutodown no executor - para encerrar a
//execucão
executor.shutdown();
}
}
The moment you submit your routine, the method ExecutorService.submit
will give you a Future
This Future object will give you access to the return value of this callable that was executed by your Thread. This will be done by calling the method Future.get()
Remember to call: executor.shutdown
- This will shut down the Pool, which will also prevent new tasks from being performed.
*Please also read the comments in the example code. They should help you understand what is happening.
The only way I know it is with recording in the database.
– Jorge B.
What do you mean? Be more specific and give an example of what you want to do.
– ramaral
First, there is no such thing as a "global variable". In fact, we are talking about "Shared Data". Data shared between multiple Threads. Probably you are referring to public and static variables. Try to improve your question, what are you doing, what don’t you understand? What do you mean "they don’t work"?
– Filipe Miranda
Can you elaborate better because you claim that they don’t work? They don’t work in what sense? I’m thinking about answering that question, but I have a lot of uncertainty as to whether the answer I’m going to write will do. I was thinking about talking about the modifier
volatile
, variables final effect, local variables vs instance vs static... But will what you really want is this?– Victor Stafusa
Hello @daniel12345smith, I will answer your question here, IE, answer as an answer to this question, because the other was blocked. I think I understand your goal: return the result value of the Thread execution.
– Filipe Miranda
I just edited your question, as soon as you approve I’ll answer.
– Filipe Miranda
@Guilhermenascimento I wrote the author’s question code myself, I wrote this wrong code there, rs. If possible, correct, or let me correct. Thank you.
– Filipe Miranda
Perfect, thanks for the tips @Guilhermenascimento Leave more detailed, in this specific case, this whole code I wrote, so I think it would be interesting to edit it myself, not the author, even why I rephrased his question. At last, thanks again.
– Filipe Miranda
@Filipegonzagamiranda edited, good night.
– Guilherme Nascimento
@daniel12345smith , the question is in agreement with your initial question?
– Math