thread与runnable的区别

    xiaoxiao2021-03-25  199

    实现多线程一般采用实现runnable接口的方式。

    thread类:

    Thread1 th1 = new Thread()1

    Thread1 th2 = new Thread()1

    Thread1 th3 = new Thread()1

    th1.start();

    th2.start();

    th3.start();

    三个线执行三个实例,没有完成资源共享。如果想完成资源共享,可以采用创建内部类继承thread类的形式。

    runnable接口:

    实现类:c1,c2,c3....

    Thread th1 = new Thread(c1);

    Thread th2 = new Thread(c2);

    Thread th3 = new Thread(c3);

    th1.start();

    th2.start();

    th3.start();

    三个线程执行同一个实例,完成资源共享

    runnable接口一般与线程池Executors.newFixedThreadPool();结合使用。

    转载请注明原文地址: https://ju.6miu.com/read-2291.html

    最新回复(0)