Java daemon threads read configuration files

Source: Internet
Author: User
Java daemon threads read configuration files
For some applications, the configuration file needs to be read in real time, but the read frequency is very high. For example, if the configuration file is modified more than 100 times per second, the frequency of modification is much lower than the read frequency. In this case, when the program is executed
When reading the configuration file, the system's Io may be under some pressure. Therefore, you can use a daemon thread to read the configuration file at intervals (for example, 1 minute, the Code directly obtains the value of the latest configuration file.
This not only reduces the frequency of reading configuration files, but also achieves better real-time performance.
The Code is as follows: Java code
  1. Import
    Java. Io. inputstream;
  2. Import
    Java. util. properties;
  3. /**
     
  4. * System Configuration File
     
  5. * @ Author sunbin
     
  6. *
     
  7. */

  8. Public
     
    Class
    Configation
    Extends
    Thread {
  9. // Static Attribute Class

  10. Private
     
    Static
    Properties P;
  11. /**
     
  12. * Default constructor
     
  13. */

  14. Public
    Configation (){
  15. //

  16. }
  17. /**
     
  18. * Inherit the methods that must be implemented by the thread
     
  19. */

  20. Public
     
    Void
    Run (){
  21. While
    (
    True
    ){
  22. // Obtain the configuration file in classpath

  23. Inputstream in = configation. Class
    . Getclassloader (). getresourceasstream (
    "Config. properties"
    );
  24. If
    (P =
    Null
    ){
  25. P = new
    Properties ();
  26. }
  27. Try
    {
  28. P. Load (in );
  29. In. Close ();
  30. Thread. Sleep (10000
    );
    // Reread the configuration file after 10 seconds of sleep

  31. } Catch
    (Exception e ){
  32. E. printstacktrace ();
  33. }
  34. }
  35. }
  36. /**
     
  37. * Get the instance of the configuration file
     
  38. * @ Return
     
  39. */

  40. Public
    Properties getproperties (){
  41. Return
    P;
  42. }
  43. /**
     
  44. * Test the main program
     
  45. * @ Param ARGs
     
  46. */

  47. Public
     
    Static
     
    Void
    Main (string [] ARGs ){
  48. Configation c = new
    Configation ();
  49. C. setdaemon (true
    );
    // Set the thread as the daemon thread

  50. C. Start (); // start the thread

  51. Try
    {
  52. Thread. Sleep (3000
    );
  53. } Catch
    (Interruptedexception e ){
  54. E. printstacktrace ();
  55. }
  56. // Print the value of the configuration file repeatedly. The value takes effect 1 second after the configuration file is modified.

  57. While
    (
    True
    ){
  58. Properties P = C. getproperties ();
  59. System. Out. println (P. getproperty ("com. test."
    ));
  60. }
  61. }
  62. }

Where C. setdaemon (true) sets the thread as the daemon thread, about the daemon thread can refer to the http://www.google.cn/search? Complete = 1 & HL = ZH-CN & Q = Java + % E5 % AE % 88% E6 % 8A % A4 % E7 % Ba % BF % E7 % A8 % 8B & meta = & AQ = T & OQ = Java + % E5 % AE % 88% E6 % 8A % A4

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.