1. Starting with JDK1.5, Java provides 3 ways to create and start Multithreading:
Ø inherit the thread class to create the threading class, overriding the run () method as the thread execution body.
Ø implement the Runnable interface to create the thread class, overriding the run () method as the thread execution body.
Ø implement the callable interface to create the thread class, overriding the run () method as the thread execution body.
The first of these is the worst, and it has 2 disadvantages:
The thread class inherits the thread class and cannot inherit other parent classes.
Because each thread is an instance of a thread subclass, it can be cumbersome to share data among multiple threads.
For the 23rd way, they are essentially the same, except that the call () method contained in the callable interface can either declare a throw exception or have a return value.
2. In addition, the start thread should use the start () method instead of the run () method. If the program never invokes the start () method of the thread object to start it, then the thread object will remain in the new State (1. New 2. Ready 3. Run 4. Block 5. Death Total 5 states), it will never be an opportunity for the thread to execute, it is just an ordinary Java object. When a program calls the run () method of a thread object, there is no difference from the normal method of invoking a normal Java object, so it will never start a new thread.
3. Static Synchronization method:
Java language rules: Before any thread enters the synchronization method, the synchronization method must be obtained before synchronizing the code block, and the synchronization monitor corresponding to the code block is synchronized. For a synchronous code block, the program must display the synchronization monitor specified for it, and for a synchronous non-static method, the synchronization monitor of the method is the Java object that this-calls the method, and for a static synchronization method, the method's synchronization monitor is not this, but the class itself.
2017 45th Monday Java Multithreading creation method