Java internal classes implement pseudo-method-level multithreading

Source: Internet
Author: User

Recently encountered a problem, that is, users fill in the relevant information after the submission, the background need to sync some files to another server, and this time, because of the spring framework, causing the front page to wait for the file synchronization to complete, in order to pop up a message. I believe that you will encounter this problem in many times, such as sending and receiving mail, but also waiting for mail to be sent, and so on. So it is natural to think of opening a thread to handle some more time-consuming tasks. But Java itself provides only the class-level multithreading, as follows:

  1. inherit the thread class
     class  A extends   Thread { public  void   run () {System.out.println ( "A" );  public  static  void   main (string[] args) {A A  = new A ();    A.run (); }}  

     

  2. Implementing the Runnable Interface
    class Implements Runnable {    publicvoid  run () {        System.out.println ("B");    }      Public Static void Main (string[] args) {        new  B ();         New Thread (b);        Thread.Start ();    }}

  3. Executor

    Can be more convenient to create a management thread pool, later I will use a blog in detail.

As you can see, the implementation of these multithreading is centralized at the class level , you must have a class to implement or inherit the appropriate interfaces and classes, and override the Run method to start using. However, suppose in one of the spring classes:

@RequestMapping ("/C") Public classC {//Main Method@RequestMapping ("C1")     Public voidC1 () {//you need to turn on another thread to call the C2 method, so you don't have to wait for C2 to finishC2 (); }    //need to implement multithreading     Public voidC2 () {//call C3C3 (); }    //file synchronization method, requires background execution     Public voidC3 () {}}

To invoke the back-end method in the C2 method, we need to call C2 in the C1 class to open another thread to execute the background business code without waiting for C3 to complete

This problem is more difficult to solve by implementing several multithreading methods above, or we create another class to implement multithreading :

 Public class Implements Runnable {    // Background business code }

Then call multithread in Class C to turn on multithreading. But since our headline is the use of internal classes, let's look at the problem of using internal classes to make it easier:

Internal classes implement pseudo-method-level multithreading:

We still use the example above to illustrate:

@RequestMapping ("/C") Public classC {//Main Method@RequestMapping ("C1")     Public voidC1 () {//you need to turn on another thread to call the C2 method, so you don't have to wait for C2 to finish//c2 (); (NewMultithreadinnerclass ()). Start (); }    //need to implement multithreading     Public voidC2 () {//call C3C3 (); }    //create internal class integration thread, call C3, turn on multithreading    classMultithreadinnerclassextendsthread{ Public voidRun {c3 (); }    }    //file synchronization method, requires background execution     Public voidC3 () {}}

This enables the implementation of pseudo-method-level multi-threading, in daily coding, multi-use internal classes will make the code logic clearer, more reasonable structure.

Java internal classes implement pseudo-method-level multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.