版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40969422/article/details/80315284
实现步骤:
(1)定义MyRunnable类实现java.lang.Runnable接口,并实现Runnable接口的run()方法,在run()方法中输出数据。
(2)创建线程对象。
(3)调用start()方法启动线程。
package cn.run.test;
//通过实现Runnable接口创建线程
public class MyRunnable implements Runnable {
int count=0;
//实现run() 方法
public void run() {
while(count<100){
count++;
System.out.println("count的值是:"+count);
}
}
}
package cn.run.test;
//启动线程
public class MyRunnableTest {
public static void main(String[] args) {
//创建线程对象
MyRunnable mt=new MyRunnable();
//Thread thread=new Thread(new MyRunnable());
Thread thread=new Thread(mt);
//启动线程
thread.start();
}
}