There are two ways to implement multithreading in Java: To inherit the thread class and implement the Runnable interface, as long as it is multi-threaded in the development of the program, it is always to realize the runnable interface, because the implementation of the Runnable interface has the following advantages over the inheritance thread class:
1, can avoid the single inheritance of Java because of the limitations caused by the characteristics;
2, enhance the robustness of the program, code can be shared by multiple threads, the code and data is independent;
3. A thread area that is suitable for multiple identical program codes handles the same resource.
The following is an example of a typical ticket-buying process (basically taking this as an example) to illustrate the difference.
The code is implemented first by inheriting the thread class:
1 2 3 4 5 6 7 8 9 () ' | class Mythread extends Th read{ private int ticket = 5 public void Run () { &nbs p; for (int i= 0;i< i++) { if (Ticket > 0) { System.out.println ("ticket =" + ticket--); } } &n