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