stay Java In language , We all know , There are two ways to create threads , One is to use Runnable Interface , The other is to use Thread class .

public class DemoRunnable implements Runnable {
public void run() {
//Code
}
}
//with a "new Thread(demoRunnable).start()" call public class DemoThread extends Thread {
public DemoThread() {
super("DemoThread");
}
public void run() {
//Code
}
}
//with a "demoThread.start()" call

There has been a lot of debate , Which is better . Um. , I'm also trying to find out , Here's my research :

1)   Realization Runnable Interface is the preferred method . ad locum , You don't need to modify Thread Class . You just give the thread something to run . It means that combination is the best way .

2)  Java Only single inheritance is supported , So you can only inherit one class .

3)   Separate your code from your thread implementation in an interface way , More clarity .

4)   Realization Runnable Make your classes more flexible . If you inherit Thread class , So what you do is always in a thread . However, if you use implementation Runnable Interface , You can run it in a thread , Or pass it to some kind of actuator (executor), Or just pass it on as a task in a single threaded application .

5)   By inheritance Thread class , Each thread has an associated unique object , To achieve Runnable Interface , Multithreading can share the same Runnable example .

6)   If you work in JDK 4 Or lower , So there's one bug:

http://bugs.java.com/bugdatabase/view_bug.do;jsessionid=5869e03fee226ffffffffc40d4fa881a86e3:WuuT?bug_id=4533087

It's in Java 1.5 It's fixed , but Sun The company is not going to fix 1.4 Medium bug.

Happy Learning !!

Realization Runnable Interfaces and inheritance Thread More about the differences between classes

  1. 【 Reprint 】JAVA There are two ways to implement threads in - Realization Runnable Interfaces and inheritance Thread class

    from : http://blog.csdn.net/sunguangran/article/details/6069317 Thank you very much to the original author , So detailed . stay java There are two ways to implement multithreading in , One is following ...

  2. Implementation of thread entry Runnable Interfaces and inheritance Thread class

    Thread 2 Method of use : Realization Runnable Interfaces and inheritance Thread class 1. Realization Runnable Interface Realization Runnable Interface , Must be realized run Method , It's also Runnable The only method in the interface class Ru ...

  3. Realization Runnable Interfaces and inheritance Thread Class differences

    If a class inherits Thread, It is not suitable for resource sharing . But if it does Runable Interface , It's easy to share resources . Realization Runnable Interface ratio inheritance Thread The advantages of class : 1): Suitable for multiple same program code ...

  4. Realization Runnable Interfaces and inheritance Thread class

    If the thread class you want to create already has a parent class , You can't inherit Thread The class ,java Multiple inheritance is not supported .   Realization Runnable Interface : package multyThread; public class MyRuna ...

  5. Thread realize Runnable Interface ratio inheritance Thread The advantages of

    1. It is suitable for multiple threads of the same program code to process the same resource , Make virtual CPU( Threads ) The same program code . Data is effectively separated , It better embodies the object-oriented design idea .2. Can be avoided by java The limitations of the single inheritance feature .3. Enhance the robustness of the program , Code ...

  6. Java Implementation of multithreading inheritance in Thread Class and Implementation Runnable Interface differences

    Java There are two ways to create threads in : 1.   By inheritance Thread class , rewrite Thread Of run() Method , Put the logic of the thread running in it 2.   By implementing Runnable Interface , Instantiation Thread class in application , ...

  7. Java In the inheritance thread Class and Implementation Runnable Interface differences

    Java There are two ways to create threads in : 1.   By inheritance Thread class , rewrite Thread Of run() Method , Put the logic of the thread running in it 2.   By implementing Runnable Interface , Instantiation Thread class in application , ...

  8. Java By inheritance thread Class and Implementation Runnable Interface to achieve the difference between multithreading

    Java There are two ways to create threads in : 1.   By inheritance Thread class , rewrite Thread Of run() Method , Put the logic of the thread running in it 2.   By implementing Runnable Interface , Instantiation Thread class One . By inheritance T ...

  9. Java Multithreading notes for basic knowledge enhancement 05:Java In the inheritance thread class And Realization Runnable Interface differences

    1. Java There are two ways to create threads in :  (1) By inheritance Thread class , rewrite Thread Of run() Method , Put the logic of the thread running in it . (2) By implementing Runnable Interface , Instantiation Thread class . 2. ...

Random recommendation

  1. High performance MySQL note : The first 1 Chapter MySQL framework

    MySQL above all . The most distinctive feature is his storage engine architecture , This architecture is designed to handle queries (Query Precessing) And its system tasks (Server Task) And data storage / Extraction phase separation .   1.1 MyS ...

  2. zabbix Automatic discovery function realizes batch production web url monitor

    demand : Now there's a lot of url Need to monitor , In the form of http://www.baidu.com , requirement url State not for 200 Call the police . Detailed analysis of requirements : a large number of url, And url Constant change , Now the monitor is zabbix, If the hand ...

  3. utilize SSIS Send E-mail

    jade-like gem Nicole Luo Blog Garden Flash memory home page New essay contact management subscribe essays - 15  article - 0  Comment on - 0  SSIS Using the send mail service Send Email Task   1.  stay SSIS How to send mail in ...

  4. poj 1330 Nearest Common Ancestors Naked LCA

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...

  5. asp.net web Development of login related operations control LoginName、LoginStatus and LoginView Control use details

    http://book.51cto.com/art/200909/154039.htm http://book.51cto.com/art/200909/154041.htm ASP.NET A set of ...

  6. Excel—— Use VLOOKUP Function to correlate data across workbooks

    Experimental environment There are two workbooks , One is < roster >, The other is < Enter the resignation form >,< roster > Details of all employees are available on the website , Include the name of the employee . department . Date of birth, etc ,< Enter the resignation form > There's resignation on the table ...

  7. [ turn ] JS Medium call() Methods and apply() Method Usage Summary

    // example 1 <script> window.color = 'red'; document.color = 'yellow'; var s1 = {color: 'blue' }; func ...

  8. fio Disk performance

    /fio -ioengine=libaio -bs=32k -direct=1 -thread -rw=randrw -percentage_random=100,0 -size=10G -filen ...

  9. Today I just learned truncate and delete The difference between , Let's summarize

    truncate table : Delete content , Release space ( The data in the table will be deleted , But not into oracle The recycle bin , Delete directly ), Don't delete the definition delete table : Delete content , Don't free up space ( The data in the table has been deleted , however ...

  10. django The relationship in the model corresponds to

    obviously , The power of relational databases is to correlate tables .Django Provides a way to define the three most common types of database relationships : For one more , Many to many and one to one . Before explanation , Let's first understand these three concepts : For one more : Two sets a,b; aggregate a Many of them ...