IT干货网

创建线程

xmjava 2022年03月12日 编程设计 186 0

创建线程

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

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


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!