黑马程序员技术交流社区

标题: 创建线程 [打印本页]

作者: zhao543    时间: 2017-3-23 17:15
标题: 创建线程
public class Test
{
    public static void main(String[] args)
    {
        NewThread thread1 = new NewThread();
        NewThread thread2 = new NewThread();
        thread1.start(); // start thread1
        thread2.start(); // start thread2
    }
}

/**
* create new thread by inheriting Thread
*/
class NewThread extends Thread {

    private static int threadID = 0; // shared by all

    /**
     * constructor
     */
    public NewThread() {
        super("ID:" + (++threadID));
    }

    /**
     * convert object to string
     */
    public String toString() {
        return super.getName();
    }

    /**
     * what does the thread do?
     */
    public void run() {
        System.out.println(this);
    }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2