多线程实现方式

2019-03-02  本文已影响0人  QQ_33e9

1、继承Thread类,重写run方法。

public class MyThread extends Thread {

    public void run() {

        System.out.println("this is my thread");

    }

}

调用:

MyThread myThread = new MyThread();

myThread.start();

2、实现Runnable接口,实现run方法。

public class MyThread implements Runnable{

    public void run() {

        System.out.println("this is my Thread");

    }

}

调用:

MyThread myThread = new MyThread();

Thread t = new Thread(myThread);

t.start();

上一篇下一篇

猜你喜欢

热点阅读