其他分享
首页 > 其他分享> > 创建线程

创建线程

作者:互联网

创建线程

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

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方法。

标签:thread,doing,创建,work,start,线程,public
来源: https://www.cnblogs.com/cxf-tech/p/15387582.html