Java – scenario of extending thread class and implementing runnable interface
•
Java
I am a novice in java thread programming, so it is a basic problem (I checked, but I haven't found this problem before)
I read that threads can be created by inheriting the thread class or by implementing the runnable interface I see a code for the same course
public class ThreadExample extends Thread implements Runnable { }
I want to know what kind of situation will want this, and if it has any advantages, what is it
Solution
It is useless to extend thread and implement runnable (thread already implements runnable) You almost always want to implement runnable (instead of extending thread) This allows you to use threads directly (not recommended) or use Java util. The flexibility of one of the newer ThreadPool implementations in concurrent (recommended)
See concurrent Java
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码