创建线程

创建线程

以下代码创建一个线程并运行:

package com.cxf.multithread.create;

public class TestForCreate {
    public static void main(String[] args) {
        new MyThread().start();
    }
}

class MyThread extends Thread{
    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println("this is doing thread work");
        }
    }
}

输出结果:

this is doing thread work
this is doing thread work
this is doing thread work
this is doing thread work
this is doing thread work

线程以类来表示,线程具体的任务放在类的run方法中,而运行这个线程通过调用类的start方法。

posted on 2021-10-09 20:51  菜小疯  阅读(18)  评论(0编辑  收藏  举报