Use synchronized to block background service processes and keep them running

Source: Internet
Author: User

When using Dubbo to start the server, you need to keep the backend running. The following figure shows that the input stream is used to block the server on the Internet:

System. In. Read (); // to ensure that the service is always on, use the congestion of the input stream to simulate it;

Find a new method and use the Lock Object class to block it so that it will not be closed

Explanation:Synchronized (XXX. Class)
Xxx. Class is a static attribute of XXX class and an object. The xxx. Class lock is used to lock the entire class. That is to say, no matter how many XXX class objects are created, these objects share the same lock tag.

Use:

<Span style = "color: # ff0000;"> synchronized (provider. Class) </span> {// wait for the thread after the server starts. The service has been running while (true) {try {provider. Class. Wait ();} catch (interruptedexception e) {system. Out. println (e );}}}
Provider is the class name, which adds a lock to this class object. When other requests come, they will wait until the end of the previous process. The complete content is as follows:

Public class provider {public static void main (string [] ARGs) throws exception {classpathxmlapplicationcontext context = new classpathxmlapplicationcontext (New String [] {"applicationcontext. XML "}); context. start (); // system. in. read (); // to ensure that the service is always on, simulate <span style = "color: # ff0000;"> synchronized (provider. class) </span> {// wait for the thread after the server starts. The service has been running while (true) {try {provider. Class. Wait ();} catch (interruptedexception e) {system. Out. println (e );}}}}}


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.