s-day05--多线程的第二种实现方式(重点)--Runnable
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.inmind.runnable_thread02;
|
||||
/*
|
||||
启动线程的第二种方式(重点):
|
||||
1.定义一个实现类实现Runnable接口
|
||||
2.实现接口的run方法(线程任务)
|
||||
3.创建出实现类对象
|
||||
4.创建线程对象将实现类对象(线程任务)作为,调用start方法
|
||||
*/
|
||||
public class Demo02 {
|
||||
public static void main(String[] args) {
|
||||
//1.创建线程任务类对象(实现类对象)
|
||||
RunnableImpl runnable = new RunnableImpl();
|
||||
//2.将任务交给线程对象,并启动线程
|
||||
Thread thread = new Thread(runnable);
|
||||
thread.start();
|
||||
System.out.println("-------");
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.println(Thread.currentThread().getName()+"-----"+i);
|
||||
}
|
||||
System.out.println("main线程结束了");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.inmind.runnable_thread02;
|
||||
|
||||
//当前类不是线程类,只是用来定义线程任务的,所以可以理解为是一个线程任务类
|
||||
public class RunnableImpl implements Runnable{
|
||||
@Override
|
||||
public void run() {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
System.out.println(Thread.currentThread().getName()+"------"+i);
|
||||
}
|
||||
|
||||
System.out.println("子线程结束了");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user