国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - java異步寫日志到文件中實現代碼

java異步寫日志到文件中實現代碼

2020-09-17 14:58nature_fly088 JAVA教程

這篇文章主要介紹了java異步寫日志到文件中實現代碼的相關資料,需要的朋友可以參考下

java異步日志文件中詳解

實現代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.tydic.ESUtil;
 
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
 
public class LogWriter {
// 日志的配置文件
  public static final String LOG_CONFIGFILE_NAME = "log.properties";
  // 日志文件名在配置文件中的標簽
  public static final String LOGFILE_TAG_NAME = "logfile";
   
  // 默認的日志文件的路徑和文件名稱
  private final String DEFAULT_LOG_FILE_NAME = "./logtext.log";
  // 該類的唯一的實例
  private static LogWriter logWriter;
  // 文件輸出流
  private PrintWriter writer; 
  // 日志文件名
  private String logFileName; 
  /**
   * 默認構造函數
   */
  private LogWriter() throws LogException{
    this.init();
  }
  private LogWriter(String fileName) throws LogException{
    this.logFileName = fileName;
    this.init();
  }
  /**
   * 獲取LogWriter的唯一實例。
   * @return
   * @throws LogException
   */
  public synchronized static LogWriter getLogWriter()throws LogException{
    if (logWriter == null){
      logWriter = new LogWriter();
    }
    return logWriter;
  }
  public synchronized static LogWriter getLogWriter(String logFileName)throws LogException{
    if (logWriter == null){
      logWriter = new LogWriter(logFileName);
    }
    return logWriter;
  }
 
  /**
   * 往日志文件中寫一條日志信息
   * 為了防止多線程同時操作(寫)日志文件,造成文件”死鎖”。使用synchronized關鍵字
   * @param logMsg  日志消息
   */
  public synchronized void log(String logMsg) {
    this.writer.println(new java.util.Date() + ": " + logMsg);
  }
  /**
   * 往日志文件中寫一條異常信息
   * 使用synchronized關鍵字。
   * @param ex  待寫入的異常
   */
  public synchronized void log(Exception ex) {
    writer.println(new java.util.Date() + ": ");
    ex.printStackTrace(writer);
  }
 
  /**
   * 初始化LogWriter
   * @throws LogException
   */
  private void init() throws LogException{
    //如果用戶沒有在參數中指定日志文件名,則從配置文件中獲取。
    if (this.logFileName == null){
      this.logFileName = this.getLogFileNameFromConfigFile();
      //如果配置文件不存在或者也沒有指定日志文件名,則用默認的日志文件名。
      if (this.logFileName == null){
        this.logFileName = DEFAULT_LOG_FILE_NAME;
      }
    }
    File logFile = new File(this.logFileName);
    try {
      // 其中的FileWriter()中的第二個參數的含義是:是否在文件中追加內容
      // PrintWriter()中的第二個參數的含義是:自動將數據flush到文件中
      writer = new PrintWriter(new FileWriter(logFile, true), true);
      System.out.println("日志文件的位置:" + logFile.getAbsolutePath());
    } catch (IOException ex) {
      String errmsg = "無法打開日志文件:" + logFile.getAbsolutePath();
      // System.out.println(errmsg);
      throw new LogException(errmsg, ex);
    }
  }
  /**
   * 從配置文件中取日志文件名
   * @return
   */
  private String getLogFileNameFromConfigFile() { 
    try
      Properties pro = new Properties(); 
      //在類的當前位置,查找屬性配置文件log.properties 
      InputStream fin = getClass().getResourceAsStream(LOG_CONFIGFILE_NAME); 
      if (fin != null){
        pro.load(fin);//載入配置文件
        fin.close(); 
        return pro.getProperty(LOGFILE_TAG_NAME);
      } else {
        System.err.println("無法打開屬性配置文件: log.properties" ); 
      }
    }catch (IOException ex) { 
      System.err.println("無法打開屬性配置文件: log.properties" ); 
    }
    return null;
  }
  //關閉LogWriter
  public void close() {
    logWriter = null;
    if (writer != null){
      writer.close();
    }
  }
 
  public static void main(String[] args) {
    LogWriter logger = null;
    try {
      String fileName = "d:/temp/logger.log";
      logger = LogWriter.getLogWriter(fileName);
//     logger.log("First log!");
//     logger.log("第二個日志信息");
//     logger.log("Third log");
//     logger.log("第四個日志信息");
      String content="tableaA|device_number|13701010";
      StringBuffer sb=new StringBuffer();
      for(int i=0;i<1000000;i++){
        sb.append(content).append(i).append(";\n");
      }
      content=sb.toString();
      long startTime=System.currentTimeMillis();
      logger.log(content);
      long endTime=System.currentTimeMillis();
      System.out.println("總消耗時間:"+(endTime-startTime));
      logger.close();
//     ReadFromFile.readFileByLines(fileName);
    } catch (LogException e) {
      e.printStackTrace();
    }
  }
}

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.tydic.ESUtil;
 
public class AychWriter extends Thread {
  private String content;
  public AychWriter(String content){
    this.content=content;
  }
  @Override
  public void run(){
    System.out.println("開始執行run()");
    LogWriter logger = null;
    String fileName = "d:/temp/logger.log";
    long startTime=System.currentTimeMillis();
    try {
      logger = LogWriter.getLogWriter(fileName);
      logger.log(this.content);
    } catch (LogException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
 
    long endTime=System.currentTimeMillis();
    System.out.println("總消耗時間:"+(endTime-startTime));
  }
}

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.tydic.ESUtil;
 
import java.io.FileWriter;
import java.io.IOException;
 
import org.junit.Test;
 
public class test_test {
  /**
   * 同步向指定文件尾部寫入字符串
   */
public void testAppendMethodB(String fileName,String content) throws IOException{
    try {
      //打開一個寫文件器,構造函數中的第二個參數true表示以追加形式寫文件
      FileWriter writer = new FileWriter(fileName, true);
      writer.write(content);
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    }
 
/**
 *調用上面同步寫方法
 */
  @Test
  public void testWriteTOFile() throws IOException{
    String fileName = "d:\\test.txt";
    String content="tableaA|device_number|13701010";
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<100000;i++){
      sb.append(content).append(i).append(";\n");
    }
    content=sb.toString();
    long startTime=System.currentTimeMillis();
    testAppendMethodB(fileName,content);
    long endTime=System.currentTimeMillis();
    System.out.println("總消耗時間:"+(endTime-startTime));
  }
  /**
   * 異步調用寫方法
   * @throws IOException
   * @throws InterruptedException
   */
  @Test
  public void testAsyncWriteTOFile() throws IOException, InterruptedException{
    String fileName = "d:\\test.txt";
    String content="tableaA|device_number|13701010";
    StringBuffer sb=new StringBuffer();
    for(int i=0;i<100000;i++){
      sb.append(content).append(i).append(";\n");
    }
    content=sb.toString();
    System.out.println("start write...");
    new AychWriter(content).start();
    System.out.println("write over...");
    Thread.sleep(30000); //重要,如果主線程掛了,調用線程也停止了
    System.out.println("main Thread over");
  }
   
}

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:http://blog.csdn.net/nature_fly088/article/details/54584802

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 在线视频a| 免费骚视频 | 欧美在线免费 | 欧美日韩精品免费 | 欧美国产一区二区 | 午夜精品电影 | 欧美精品一区二区三区手机在线 | 精品日韩一区二区三区 | 日本亚洲最大的色成网站www | 日本一区二区三区在线视频 | 9999777做爰 | 激情综合久久 | 九九只有精品 | 国产在线观看免费 | 99久久夜色精品国产网站 | 男女精品视频 | 爱操av| 国产亚洲一区二区精品 | 二区中文字幕 | 午夜视频污 | 毛片免费视频 | 欧美日韩精品免费 | 天天澡天天狠天天天做 | 欧美视频三区 | 国产精品久久久久久久午夜片 | 亚洲一区精品在线 | 色偷偷噜噜噜亚洲男人 | 色噜噜狠狠狠综合曰曰曰 | 日韩视频免费 | 日韩免费电影 | 人人爱av| 免费黄色大片 | 欧美区国产区 | 国产精品毛片久久久久久久 | 精品国产一区二区三区性色av | 国产精品无码永久免费888 | 欧美一区二区三区视频 | 久久夜视频 | 啪啪导航 | 成人久久久 | 一级黄色大片 |