简体   繁体   中英

Java: Should I be logging issues in the event dispatch thread (EDT)?

I was wondering if it is acceptable to be writing to a log file directly in the event dispatch thread. I know a rule of thumb is to keep long operations out of the EDT, however I am unsure if a single write to a text file would fall into this category. I currently have worker threads doing this but I am running into some strange exceptions. Before researching these exceptions I thought I would see if it would be ok to just do these log writes directly in the EDT. As I said, these writes are relatively small and infrequent.

Thanks in advance!

I would suggest dispatching this to SwingWorker . This will hand the actual file write off to a background thread. However, if you are using a framework such as log4j, you should investigate what buffering or other multithreading the library performs first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM