There are three ways to create threads: one is to inherit the thread class, the other is to implement the Runnable interface, and the third is to create threads using callable and future.
Inheriting the thread class to create threads
Steps:
- Defines the thread subclass and overrides the run method of the class, which represents the task to be completed, that is, the thread execution body
- Create a thread instance
- Call the Start () method of the thread subclass to start the thread
1 Public classThreadTestextendsthread{2 3 Private inti;4 5 Public voidrun () {6 7 for(; i < i++; ) {8 //GetName () returns the name of the current thread9System.out.println (GetName () + "" +i);Ten } One A } - - the Public Static voidMain (string[] args) { - - for(inti = 0; I < 100; i++){ - //gets the current thread + System.out.println (Thread.CurrentThread (). GetName () -+" " +i); + if(i = = 20){ A Newthreadtest (). Start (); at Newthreadtest (). Start (); - } - } - } - -}
Implementing the Runnable Interface creation thread
Steps:
- Define the Runnable implementation class and override the annoying run method of the class, which is also the thread execution body "
- Create an instance of the Runnable subclass and use this as the target of the thread to create the thread object, which is the true thread object
1 Public classThreadTestImplementsrunnable{2 3 Private inti;4 5 Public voidrun () {6 7 for(; i < i++; ) {8 //When a thread implements a class with runnable, if you want to get the current thread9 //only with Thread.CurrentThread ()Ten System.out.println (Thread.CurrentThread (). GetName () One+" " +i); A } - - } the - - Public Static voidMain (string[] args) { - + for(inti = 0; I < 100; i++){ - //gets the current thread + System.out.println (Thread.CurrentThread (). GetName () A+" " +i); at if(i = = 20){ -ThreadTest st=Newthreadtest (); - //Create a real thread object from the new thread (target, name) - NewThread (ST, "Threads 1")). Start (); - NewThread (ST, "Threads 2")). Start (); - } in } - } to +}
Creating threads using callable and future
View http://blog.csdn.net/ghsau/article/details/7451464
Three ways to create threads compare
When using the runnable, callable interface to transcend multiple threads, the advantages are:
The thread class simply implements the Runnable interface or callable interface and can inherit other classes.
In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which is a good embodiment of object-oriented thinking.
Disadvantages are:
Programming is slightly more complex, and you must use the Thread.CurrentThread () method if you want to access the current thread.
The advantage of creating multiple threads with the inherited thread class is:
Writing is simple and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.
Disadvantages are:
The thread class has inherited the thread class, so it is no longer possible to inherit other parent classes.
Java Thread (ii) Create and start