Spring Asynchronous operation

Source: Internet
Author: User

The asynchronous operation of Java is implemented through multithreading.

Two ways to implement a thread:

1. Customizing a class, inheriting thread, overriding the run () method of the Thread class

2. Customize a class to implement the Runnable interface and implement the run () method.

Thread mode:

Example

public class MyThread extends Thread {

/** rewrite thread's Run () */
public void Run () {
/** the actions performed by the thread */
System.out.println ("Hello thread");
}

public static void Main (string[] args) {
Thread thread = new MyThread ();
Start thread
Thread.Start ();
}
}

Runable Implementation Method:

(1). Example

public class Myrunnable implements Runnable {

/** implementing the Run () method */
@Override
public void Run () {
/** The actions performed by the thread */
System.out.println ("Hello Runnable");
}

public static void Main (string[] args) {
Thread thread = new Thread (new myrunnable ());
Thread.Start ();

}
}
Thread thread1 = new Thread (new Runnable () {
@Override
public void Run () {
System.out.println ("Hello");
}
});
Thread1.start ();

Asynchronous:

In the most primitive way, when we are going to execute a task in parallel or asynchronously, we use the above method to start a thread.

Disadvantages:

1. Bad performance for new thread

2, the thread lacks the unified management, may the unrestricted new thread, competes with each other, may also occupy excessive system resources to cause the freezing or oom (out of Memory);

Spring asynchronous operations: Using the thread pool: taskexecutor

Taskexecutor implementation has many, this time only for specific functions, the use of the implementation of Threadpooltaskexecutor

The following actions are available when the spring environment is working:

1. Configure in the XML file:

   <bean id= "Taskexecutor"
Class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
P:corepoolsize= "5" p:maxpoolsize= "ten" p:queuecapacity= "100"
P:waitfortaskstocompleteonshutdown= "true"/>

2. Introduction of the Bean in the class

    @Resource (name= "Taskexecutor")
Private Taskexecutor Taskexecutor;

3. Implementation

Taskexecutor.execute (Runnable Runnable);
Taskexecutor.execute (New Runnable () {
public void Run () {
/** the operations performed by the thread x/
try {
Sendpushiservice.sendpush (Pushparams);
} catch (Serviceexception e) {
Log.error ("Send push error, the cause:" + e.getmessage ());
}
}});




Spring Asynchronous operation

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.