实现多线程一般采用实现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();结合使用。
本文介绍了在Java中实现多线程的两种方式:通过继承Thread类和实现Runnable接口。重点讲解了如何利用Runnable接口来实现线程间的资源共享,并指出这种方式常与线程池结合使用。
2141

被折叠的 条评论
为什么被折叠?



