实现Runnable接口的方式实现多线程

package Thread.Thread04;

/**
* FileName: MyThread
* Author: lps
* Date: 2022/3/29 16:35
* Sign:刘品水 Q:1944900433
*/
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+","+i);
}
}
}


package Thread.Thread04;

/**
* FileName: MyRunnableDemo
* Author: lps
* Date: 2022/3/29 16:37
* Sign:刘品水 Q:1944900433
*/
public class MyRunnableDemo {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();

//Thread(Runnable target)
//分配一个新的 Thread对象。
//Thread(Runnable target, String name)
//分配一个新的 Thread对象。



Thread t1 = new Thread(myRunnable,"飞机");
Thread t2 = new Thread(myRunnable,"火炬");


t1.start();
t2.start();


}
}

 

posted @ 2022-03-30 15:09  刘品水  阅读(45)  评论(0)    收藏  举报