Objective
Multithreading is often encountered in our development process, but also necessary to master. The first thing we need to know when we need to do multithreaded development is how to implement multithreading, which is how we should create threads.
Creating a thread in Java is the same as an object operation that creates an ordinary class, and we can create a thread in two ways:
1, inherit the thread class, and rewrite the Run () method.
2, implement the Runnable interface, and implement the Run () method.
method One: inherit the Thread class
The code is very simple
First overload a constructor so that we can name the thread.
Override the Run () method.
Here we first let the thread output the thread name +start.
Then each 5ms output thread name + an increment number.
/**
* 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 Two: implement Runnable interface
The code is also very simple
Implement the Run () method.
Here we first let the thread output the thread name +start.
Then each 5ms output thread name + an increment number.
/**
* 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!
Threadthread 0
Runnablethread start!
Runnablethread 0
threadthread 1
runnablethread 1
threadthread 2
runnablethread 2
Threadthread 3
runnablethread 3
threadthread 4
runnablethread 4
threadthread 5
Runnablethread 5
threadthread 6
runnablethread 6
threadthread 7
runnablethread 7
Threadthread 8
runnablethread 8
threadthread 9
runnablethread 9
Comparison of two methods
1. Because Java only supports single inheritance, you can no longer inherit other classes using method one, and method two implements interfaces without affecting inheriting other classes.
2. method as a result of the inheritance thread, so direct new out can start, and method two need to pass the object as a parameter into the thread object to get the thread object.
3. In method one, the thread name can be obtained directly through This.getname, and method two needs Thread.CurrentThread (). GetName () obtained
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.