thread与runnable的区别

来源:互联网 发布:那些网络歌手成名歌 编辑:程序博客网 时间:2024/05/05 06:01

实现多线程一般采用实现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();结合使用。

0 0
原创粉丝点击