编程那点事编程那点事

专注编程入门及提高
探究程序员职业规划之道!

通过继承Thread类实现多线程

万物皆对象,那么线程也是对象,对象就应该能够抽取其公共特性封装成为类,使用类可以实例化多个对象,那么实现线程的第一种方式就是继承Thread类的方式。继承Thread类是最简单的一种实现线程的方式,通过jdk给我们提供的Thread类,重写Thread类的run方法即可,那么当线程启动的时候,就会执行run方法体的内容。代码如下:

package com.hy.thread.t1;
/**
 * 继承Thread类的方式实现多线程演示
 */
public class ThreadDemo extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();
        td.start(); // 启动线程
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果如下

main is running ...
Thread-0 is running ...
main is running ...
Thread-0 is running ...
Thread-0 is running ...
main is running ...
Thread-0 is running ...
main is running ...

这里要注意,在启动线程的时候,我们并不是调用线程类的run方法,而是调用了线程类的start方法。那么我们能不能调用run方法呢?答案是肯定的,因为run方法是一个public声明的方法,因此我们是可以调用的,但是如果我们调用了run方法,那么这个方法将会作为一个普通的方法被调用,并不会开启线程。这里实际上是采用了设计模式中的模板方法模式,Thread类作为模板,而run方法是在变化的因此放到子类。

创建多个线程

上面的例子中除了我们创建的一个线程以外其实还有一个主线程也在执行。除了这两个线程以外还有没有其他的线程在执行了呢,其实是有的,比如我们看不到的垃圾回收线程,也在默默的执行。这里我们并不去考虑有多少个线程在执行,上面我们自己创建了一个线程,那么能不能多创建几个一起执行呢,答案是肯定的。一个Thread类就是一个线程对象,那么多创建几个Thread类,并调用其start方法就可以启动多个线程了。代码如下

package com.hy.thread.t1;
public class MultiThreadDemo extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        // 创建四个线程对象,代表四个线程
        MultiThreadDemo td1 = new MultiThreadDemo();
        MultiThreadDemo td2 = new MultiThreadDemo();
        MultiThreadDemo td3 = new MultiThreadDemo();
        MultiThreadDemo td4 = new MultiThreadDemo();
        td1.start(); // 启动线程
        td2.start(); // 启动线程
        td3.start(); // 启动线程
        td4.start(); // 启动线程
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行结果如下

main is running ... 
Thread-2 is running ... 
Thread-1 is running ... 
Thread-3 is running ... 
Thread-0 is running ... 
Thread-3 is running ... 
Thread-2 is running ... 
main is running ... 
Thread-1 is running ... 
Thread-0 is running ... 
Thread-1 is running ... 
main is running ... 
Thread-2 is running ... 
Thread-0 is running ... 
Thread-3 is running ...

我们发现这里有个问题,多个线程的名字都是系统定义好的,就是Thread-开头,后面跟数字,如果我们每个线程处理不同的任务,那么我们能不能给线程起上不同的名字,方便我们排查问题呢?答案是可以的。只要在创建线程实例的时候,在构造方法中传入指定的线程名称即可。如下

package com.hy.thread.t1;
public class MultiThreadDemo extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 指定线程名称的构造方法
     * 
     * @param name
     */
    public MultiThreadDemo(String name) {
        super(name);
    }
    public static void main(String[] args) {
        // 创建四个线程对象,代表四个线程
        MultiThreadDemo td1 = new MultiThreadDemo("t1"); // 指定线程的名字
        MultiThreadDemo td2 = new MultiThreadDemo("t2");
        MultiThreadDemo td3 = new MultiThreadDemo("t3");
        MultiThreadDemo td4 = new MultiThreadDemo("t4");
        td1.start(); // 启动线程
        td2.start(); // 启动线程
        td3.start(); // 启动线程
        td4.start(); // 启动线程
        while (true) {
            System.out.println(Thread.currentThread().getName() + " is running ... "); // 打印当前线程的名字
            try {
                Thread.sleep(1000); // 休息1000ms
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

运行的结果如下:

main is running ... 
t1 is running ... 
t2 is running ... 
t3 is running ... 
t4 is running ... 
main is running ... 
t1 is running ... 
t2 is running ... 
t4 is running ... 
t3 is running ...


未经允许不得转载: 技术文章 » Java编程 » 通过继承Thread类实现多线程