-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66ecce4
commit b3694c7
Showing
3 changed files
with
91 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package utils; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import static utils.Constants.LOG_FILE_NAME; | ||
|
||
/** | ||
* 用于线程安全地写入文件 | ||
* 该类为单例 | ||
* Created by ZQ on 2016/4/30. | ||
*/ | ||
public class LogHelper { | ||
private List<String> logList = new CopyOnWriteArrayList<>(); | ||
private ExecutorService exec; | ||
|
||
private LogHelper(){ | ||
exec = Executors.newSingleThreadExecutor(); | ||
} | ||
|
||
private static class Holder{ | ||
private static LogHelper instance = new LogHelper(); | ||
private static LogHelper getInstance(){ | ||
return instance; | ||
} | ||
} | ||
|
||
public static LogHelper getInstance(){ | ||
return Holder.getInstance(); | ||
} | ||
|
||
public void addLog(String log){ | ||
exec.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
FileUtils.writeFileAppend(LOG_FILE_NAME, log); | ||
} | ||
}); | ||
} | ||
|
||
|
||
} |