ThreadFactory usage in Java

前端 未结 10 1912
旧巷少年郎
旧巷少年郎 2020-12-02 05:08

Can someone briefly explain on HOW and WHEN to use a ThreadFactory? An example with and without using ThreadFactory might be really helpful to understand the differences.

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 05:43

    ThreadFactory will be useful

    • for setting more descriptive thread name
    • to set thread daemon status
    • for setting thread priority

    You could use ThreadFactoryBuilder from google Guava lib to create ThreadFactory like this

    ThreadFactory threadFactory = new ThreadFactoryBuilder()
            .setNameFormat("MyThreadPool-Worker-%d")
            .setDaemon(true)
            .build();
    

提交回复
热议问题