If you use multithreading, you can run multiple processes in parallel.
Each of these processes is called a thread.
There are two ways to perform thread processing in Java, one is to use inheritance (extends
) and the other is to implement an interface (implements
).
In the former case, inherit the Thread
class and call thestart ()
method.
In the latter case, implement the Runnable
interface and call therun ()
method.
Since multiple inheritance is not possible in Java, other classes cannot be inherited by the method using Thread
, but it is possible by the method using the interface.
In the latter case, you can also implement the Callable
interface and call thecall ()
method.
In the case of Runnable
, the return value cannot be specified and exceptions cannot be skipped.
Runnable_interface
public interface Runnable {
public abstract void run();
}
Implement it like this. Override the run ()
method.
Runnable_implements
static class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("It's Runnable");
}
}
In the case of Callable
, the return value can be specified and an exception can be skipped.
Callable_interface
public interface Callable<V> {
V call() throws Exception;
}
Implement it like this. Override the call ()
method.
The <> part is called generics, and you can specify the return type. (String below)
Callable_implements
static class MyCallable implements Callable<String> {
@Override
public String call() throws Exception {
String result = "Callable";
return result;
}
}
I want to create an object and use Callable
.
main
MyCallable myCallable = new MyCallable();
It is convenient to use Future
and ExecutorService
here.
main
//Create an ExecutorService instance
ExecutorService executorService = Executors.newSingleThreadExecutor();
//Ask the Executor to execute the thread
Future<String> future = executorService.submit(myCallable);
//Get to retrieve the result()Use method
String result = future.get(100, TimeUnit.MILLISECONDS);
Execute myCallable
via ExecutorService
and store the result in a variable of type Future
.
The return type is specified by generics. In the above, it is set to String
.
The result is retrieved using the get ()
method of Future
.
Also, by specifying the argument as above, you can raise an exception if it takes 0.1 seconds or more.
For me, who doesn't understand well
** "Can't you just callmyCallable.call ()
directly like you did with Runnable?" **
I thought,
Since the processing of myCallable
is done in a separate thread, you have to wait for the return value to be returned after the processing is completed.
I think that if you use ExucutorService
, you can get the return value via the future
object after waiting until the processing is completed properly. (Maybe)
The name Future
is also futuristic.
Since the static member of class is managed as a shared resource, it can be rewritten by another thread.
It is convenient to use ThreadLocal
to complete within the threat.
main
//Create an instance of ThreadLocal
private final ThreadLocal<ExecutorService> executorServicePool = new ThreadLocal<>();
//Set SingleThreadExecutor
executorServicePool.set(Executors.newSingleThreadExecutor());
//Get to retrieve the result()Use method
ExecutorService executorService = executorServicePool.get();
This makes it more thread-safe.
Recommended Posts