线程的两种实现方式。

来源:互联网 发布:oracle数据迁移方式 编辑:程序博客网 时间:2024/05/21 17:56

一,通过继承thread类来实现。

步骤:创建一个类继承thread

2.重写run方法。

3.创建thread的对象。

4.通过.start开启线程。

package arrary;public class thread {public static void main(String[] args) {mythread ny = new mythread();ny.start();}}class mythread extends Thread{public void run() {//重写run方法,在run方法里写逻辑。System.out.println(6);}}
二,通过实现runable接口来实现。

1.创建一个类来实现runable接口

2.重写run方法。

3.创建thread对象

4.通过.start开启线程。

package arrary;public class thread {public static void main(String[] args) {mythread tn = new mythread();Thread t = new Thread(tn);t.start();}}class mythread implements Runnable{public void run() {System.out.println(8);}}



原创粉丝点击