Everybody is good, today to talk to you the inside of the Java multi-thread concurrent programming, the multi-threaded concurrent programming, we all know, to put it bluntly, is to open a new thread in the code to perform a piece of code, and then when the code is complete, you may also not too clear, but he’s always after a period of time has been completed.

How is the code run?

So let’s take a look at how this so-called multi-threaded concurrent programming works. In fact, we have to start with the main method. Basically, if you write a piece of Java code, generally speaking, you have to run a main method to start and execute Java code, right? Spring Boot is also based on the main method.

So what’s the first thing you do when you run the code? Instead, it starts a JVM process, which loads your class, starts running your main method, and everything else you’ve written. In the process, it simply loads whatever class it needs from the file on disk into memory, as shown below.

So here’s a question for you to think about: how does a JVM process run main? Does the JVM process execute the code in main itself? The JVM process has a default thread called the main thread, which is responsible for running our main method code, as shown below.

What is multithreaded programming?

So what do we mean by multithreaded programming? Even easier, if you don’t have multiple threads, by default, it’s the main thread and one thread runs your main method and all the code that follows.

If you want to start more threads running code at the same time, you can use new Thread().start() to start a Thread, and that Thread will run its part of the code concurrently. Note that multiple threads can run concurrently. This means that the main Thread and the newly opened Thread run concurrently, almost at the same time, as shown below.

So the problem is, for your main thread, you open a thread to execute some code, but the problem is, you want to wait until the thread is finished to give you a value, but you don’t know when the thread is finished, You also don’t know how the thread will give you its return value. In other words, you lack some control between the main thread and the Thread, as shown below.

Get the thread return value based on FutureTask

So in this case, when we’re playing with multithreaded concurrent programming, we have to introduce the Future, and the Future, in fact, represents your control over another thread, and when you start a thread and it runs, if you can get a Future, You can use the Future to control that thread, for example, interrupt that thread, get the return value of that thread, and so on, as shown below.

So this Future is a must to master when we write multithreaded concurrent programming in Java, because it is often used! Let’s take a look at how the Future is used in code. First, we write some code for the task to be executed by the thread child, as follows:

public class Task implements Callable<String> {
  
  public String call(a) throws Exception {
    // Execute a piece of task code, get a result, and return
    System.out.println("Simulate running task code");
    // The default task code takes 500ms to run
    Thread.sleep(500); 
    String result = "Simulation return result";
    returnresult; }}Copy the code

Use FutureTask to start a thread running the code in the main method, and use the Future method to retrieve the result of the thread running the code as follows:

public class FutureTaskTest {
  
  public static void main(String[] args) 
  		throws InterruptedException, ExecutionException {
    // Build a FutureTask based on the task code we wrote ourselves
    // It's actually a task, but the FutureTask provided by the JDK encapsulates our task code
    FutureTask<String> futureTask = 
      new FutureTask<String>(new Task());
    // Build a thread pool, which will have a thread that actually runs the task
    ExecutorService threadPool = Executors.newFixedThreadPool(1);
    // Submit the FutureTask to the thread pool and let the thread pool run our task code
    threadPool.submit(futureTask);
    
    // This is a place where we can simulate doing something else, executing a lot of other code, after a while
    Thread.sleep(1000);
    
    // After some time, the thread in the thread pool should have finished running our submitted task code
    // FutureTask can then be used to retrieve the result of that task's code being runSystem.out.println(futureTask.get()); }}Copy the code

conclusion

As you can see from the above code, when executing a task code with a child thread, the task code can return a value when it is finished. Then we can encapsulate the task code with FutureTask, and after a certain period of time, Use FutureTask to get the return value of the task code after it is completed. This is a common programming technique for Java multithreaded concurrent programming. I hope you can get the magic use of this Future today.

END

pleasantly surprised