用Thread类创建线程

来源:互联网 发布:下载文件进度条js特效 编辑:程序博客网 时间:2024/05/21 02:48

创建线程有两种方法。继承Thread类或者实现runnable接口。
下面用Thread简单创建两个线程:

public class Thread1 extends Thread {    public void run(){        System.out.println(this.getName());    }    public static void main(String[] args) {        System.out.println(Thread.currentThread().getName());        Thread1 thread1 = new Thread1();        Thread1 thread2 = new Thread1();        thread1.start();        thread2.start();    }}

结果:

mainThread-0Thread-1

由于没有指定线程名字,输出的是线程的默认值Thread-n的形式,main是主线程。

0 0
原创粉丝点击