Preface
Multithreading is something we often encounter during the development process and is also an indispensable and necessary mastery. When we know that we need to do multi-threading development, the first thing we need to know is naturally how to implement multi-threading, that is, how we should create threads.
Creating threads in Java is the same as creating object operations for ordinary classes. We can create threads in two ways:
1. Inherit the Thread class and override the run() method.
2. Implement the Runnable interface and implement the run() method.
Method 1: Inherit the Thread class
The code is very simple
First overload a constructor so that we can name the thread.
Rewrite the run() method.
Here we first let the thread output the thread name + start.
Then output the thread name + an incremental number every 5ms.
/** * Created by holten.gao on 2016/10/17. */public class threadThread extends Thread { public threadThread(String name) { super(name); } @Override public void run() { System.out.println(this.getName()+" start!"); for(int i=0;i<10;i++){ System.out.println(this.getName()+" "+i); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } }}Method 2: Implement the Runnable interface
The code is also very simple
Implement the run() method.
Here we first let the thread output the thread name + start.
Then output the thread name + an incremental number every 5ms.
/** * Created by holten.gao on 2016/10/17. */public class runnableThread implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()+" start!"); for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()+" "+i); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } }}Test results
Test code
/** * Created by holten.gao on 2016/10/17. */public class Main { public static void main(String[] args) { Thread threadThread=new threadThread("threadThread"); threadThread.start(); Thread runnableThread=new Thread(new runnableThread(),"runnableThread"); runnableThread.start(); }}Test results
threadThread start!threadThreadThread 0runnableThread start!runnableThread 0threadThread 1runnableThread 1threadThread 2runnableThread 2threadThread 3runnableThread 3threadThread 4runnableThread 4threadThread 5runnableThread 5threadThread 6runnableThread 6threadThread 7runnableThread 7threadThread 8runnableThread 9
Comparison of the two methods
1. Because Java only supports single inheritance, using method one cannot inherit other classes; while implementing interfaces in method two will not affect inheritance of other classes.
2. Method 1: Since it inherits Thread, you can start by directly new; while Method 2: You need to pass the object as a parameter into the Thread object to obtain the Thread object.
3. In method one, you can directly obtain the thread name through this.getName; while in method two, you need to obtain the thread.currentThread().getName().
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.