创建线程的2种方式

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了创建线程的2种方式脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

线程创建的2种方式

1. 继承Thread类

public class FactorialThreadTester {
    public static void main(String[] args) {
        System.out.println("main thread starts");
        FactorialThread thread = new FactorialThread(10);
        
        thread.start();
        System.out.println("main thread ends");
    }
}
class FactorialThread extends Thread {
    private int num;
    public FactorialThead(int num) {
        this.num = num;
    }
    
    public void run() {
        int i = num;
        int result = 1;
        System.out.println("new thread started");
        
        while(i > 0) {
            result = result * i;
            i = i - 1;
        }
        
        System.out.println("The factorial of " + num + "is " + result);
        System.out.println("new thread ends");
    }
}

2. 实现Runnable接口

https://www.xuetangx.com/learn/THU08091000252/THU08091000252/7754101/video/12732923

  • Runnable接口

    只有一个 run()方法

    Thread类 实现了 Runnable接口

    便于多个线程 共享资源

    Java不支持多继承, 如果已经继承了某个基类, 便需要实现 Runnable 接口 来生成 多线程

    以实现 Runnable 的对象 为 参数, 建立新的线程

    start方法 启动线程, 就会运行 run()方法

2.1. Runnable接口, 实现单一线程

public class FactorialThreadTester {
    public static void main(String[] args) {
        System.out.println("main thread starts");
        
        FactorialThread t = new FactorialThread(10); // 实现了Runnable的类
        new Thread(t).start(); // 运行 FactorialThread 的 run
        
        System.out.println("new thread started, main thread ends");
    }
}
class FactorialThread implements Runnable {
    private int num;
    public FactorialThread(int num) {
        this.num = num;
    }

    public void run() {
        int i = num;
        int result = 1;
        while (i > 0) {
            result = result * i;
            i = i - 1;
        }
        
        System.out.println("The factorial of " + num + "is" + result);
        System.out.println("new thread ends");
    }

}

2.2. Runnable接口, 实现多个线程

public class ThreadSleepTester {
    public static void main(String[] args) {
        TestThread thread1 = new TestThread();
        TestThread thread2 = new TestThread();
        TestThread thread3 = new TestThread();
        System.out.println("Starting threads");
        
        new Thread(thread1, "Thread1").start();
        new Thread(thread2, "Thread2").start();
        new Thread(thread3, "Thread3").start():
        
        System.out.println("Threads started, main endsn");
    }
}
class TestThread implements Runnable {
    private int sleepTime;
    public TestThread() {
        sleepTime = (int) (Math.random() * 6000);
    }
    
    public void run() {
        try {
            System.out.println(Thread.currentThread().getName() + "going to sleep for " + sleepTime);
            
            Thread.sleep(sleepTime);
        } catch (InterruptedException exception) {};
        
        System.out.println(Thread.currentThread().getName() + "finished");
    }
}

2种线程构造方式的比较

  • 使用Runnable接口

    可以将 CPU, 代码, 数据 分开, 形成清晰的模型;

    还可以从其他类继承

  • 直接继承Thread类

    编写简单, 直接继承, 重写run方法, 不能再从其他类继承

脚本宝典总结

以上是脚本宝典为你收集整理的创建线程的2种方式全部内容,希望文章能够帮你解决创建线程的2种方式所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: