多线程第二种,实现Runnable接口

package com.msb.test02;

/**
 * @author : liu
 * 日期:10:27:43
 * 描述:TestThread实现了这个接口,才会变成一个线程类
 * 版本:1.0
 */
public class TestThread implements Runnable{
    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            System.out.println(Thread.currentThread().getName()+"----"+i);
        }
    }
}
package com.msb.test02;

/**
 * @author : liu
 * 日期:10:30:07
 * 描述:IntelliJ IDEA
 * 版本:1.0
 */
public class Test {
    //这是一个main方法:是程序的入口
    public static void main(String[] args) {
        TestThread tt = new TestThread();
        Thread t=new Thread(tt,"子线程");
        t.start();
        //主线程里面打印1-10数字
        for (int i = 1; i <=10 ; i++) {
            System.out.println(Thread.currentThread().getName()+"-----"+i);
        }
    }
}

运行结果

 

posted @ 2022-11-19 10:36  爱的加勒比  阅读(18)  评论(0)    收藏  举报