I've written a basic post that creates multithreading:
So this blog post is mainly to compare the differences between the two creation threads.
Inheritance Thread class:
Also take the example of the blog post:
Four threads each sell their own tickets, stating that four threads are not shared and are independent threads. Each line accesses tile and so on, there is no priority relationship. These three threads are not an alternating execution, but three threads are executed at the same time, some of the threads are allocated the opportunity of the chip, the ticket is sold out in advance, some threads are allotted time slice of the opportunity less, the ticket is sold out later.
Therefore, the use of extended Thread The multiple threads created by the class, while executing the same code, are independent of each other and have their own resources and do not interfere with each other.
Analysis Thread type of source code:
1 ) thread runnable So it implements the runnable in interface run method
2 ) when a thread object is generated, the name of the thread object will use the following form if it is not set to a name: thread-number, The number will be automatically incremented and be all thread object is shared (because it is static
3 ) We need to rewrite Run method, because at this point Run methods do not do anything.
4) when usingRunnablemethod to generate the thread object, we need to implement theRunnableinterface ofRunmethod, usingNew Thread(new MyThread ()) to generate the thread object, at which point the thread object'sRunMethod InvocationMyThreadClass ofRunmethod, so that we write our ownRunmethod is executed.
Implement Runnable Interface:
by implementing Runnable interface for resource sharing between threads.
Inheritance Thread classes and implementations Runnable the difference between the interfaces:
Adoption of inheritance Thread class implements Multithreading:
Advantage: Easy to write, if you need access to the current thread, just use the This no need to use Thread.CurrentThread () method.
disadvantage: Because this thread class has inherited the Thread class, so you can no longer inherit other classes.
Implement Runnable Interface:
Advantage: Threads simply implement interfaces and can inherit other classes.
in this case, you can enable multiple threads to share a Target object, so it is well suited for situations where multiple threads are used to handle the same resource, which allows CPU , code and data separate, form a clear model, better reflect the object-oriented thinking.
disadvantage: Programming is slightly more complex, and if you need access to the current thread, you must use Thread.CurrentThread () method.
the implementation of the Runnable interface has unparalleled advantages over the extended Thread class. This approach is not only beneficial to the robustness of the program, so that the code can be shared by multiple threads, and the code and data resources are relatively independent, which is particularly suitable for multiple threads with the same code to handle the same resource situation. In this way, the thread, code and data resources of the effective separation, well embodies the object-oriented programming ideas. So almost all multithreaded programs are done by implementing the Runnable interface.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Multithreaded programming--Creating threads thread VS Runnable