java中实现多线程的方式有两种:
1.是继承Thread类。
2.是实现Runable接口。
3.实现Callable接口,通过Executor执行器执行。
Runnable接口只有一个run()方法。
public
interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
平常我们是通过Runnable方式实现多线程,其实Thread就是Runnable的一个实现类,可以查看Thread的源码。
public
class Thread implements Runnable {
/* Make sure registerNatives is the first thing <clinit> does. */
private static native void registerNatives();
static {
registerNatives();
}
private char name[];
private int priority;
private Thread threadQ;
private long eetop;
/* Whether or not to single_step this thread. */
private boolean single_step;
/* Whether or not the thread is a daemon thread. */
private boolean daemon = false;
/* JVM state */
private boolean stillborn = false;
/* What will be run. */
private Runnable target;
/* The group of this thread */
private ThreadGroup group;
/* The context ClassLoader for this thread */
private ClassLoader contextClassLoader;
/* The inherited AccessControlContext of this thread */
private AccessControlContext inheritedAccessControlContext;
/* For autonumbering anonymous threads. */
private static int threadInitNumber;
....
public void run() {
if (target != null) {
target.run();
}
}
1.通过继承Thread的方式,只需要继承Thread类,然后重写run方法,把线程运行的代码放在其中,调用start方法启动线程。
public class Main {
public static void main(String arg[]){
Person p1=new Person("p1");
Person p2=new Person("p2");
p1.start();
p2.start();
}
}
class Person extends Thread {
private String name;
Person(String s){
name=s;
}
@Override
public void run() {
for(int i=0;i<50;i++)
System.out.println(name+" is running");
}
}
Thread start方法如下
public synchronized void start() {
/**
* This method is not invoked for the main method thread or "system"
* group threads created/set up by the VM. Any new functionality added
* to this method in the future may have to also be added to the VM.
*
* A zero status value corresponds to state "NEW".
*/
if (threadStatus != 0)
throw new IllegalThreadStateException();
group.add(this);
start0();
if (stopBeforeStart) {
stop0(throwableFromStop);
}
}
这里主要是调用了start0方法,这时一个native本地调用,底层使用c语言实现
private native void start0();
2.实现Runnable接口
public class Main {
public static void main(String arg[]){
Runnable p1=new Person("p1");
Runnable p2=new Person("p2");
new Thread(p1).start();
new Thread(p2).start();
}
}
class Person implements Runnable {
private String name;
Person(String s){
name=s;
}
@Override
public void run() {
for(int i=0;i<50;i++)
System.out.println(name+" is running");
}
}
实现方式和继承Thread相似,不过Runnable没有Start方式,所以通过创建一个Thread对象,Runnable相当于代码的逻辑部分,现在的执行、管理、结束交给Thread对象负责。
public Thread(Runnable target) {
init(null, target, "Thread-" + nextThreadNum(), 0);
}
Thread中有Runnable接口实现的引用;
private Runnable target;
当然还有其他的构造函数通过转换。
由于在JAVA中只能单继承,所以使用实现Runnable接口的方式更好,在实际开发中这种方式用的也比较多,这种方式也可以使业务逻辑和线程管理相分离。