在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable,可以实现资源共享
来源:互联网 发布:校园翼讯mac 编辑:程序博客网 时间:2024/05/29 05:08
在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:
->避免点继承的局限,一个类可以继承多个接口。
->适合于资源的共享
以卖票程序为例,通过Thread类完成:
package org.demo.dff;
class MyThread extends Thread{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣票:ticket"+this.ticket--);
}
}
}
};
下面通过三个线程对象,同时卖票:
package org.demo.dff;
public class ThreadTicket {
public static void main(String[] args) {
MyThread mt1=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();
mt1.start();//每个线程都各卖了10张,共卖了30张票
mt2.start();//但实际只有10张票,每个线程都卖自己的票
mt3.start();//没有达到资源共享
}
}
如果用Runnable就可以实现资源共享,下面看例子:
package org.demo.runnable;
class MyThread implements Runnable{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣票:ticket"+this.ticket--);
}
}
}
}
package org.demo.runnable;
public class RunnableTicket {
public static void main(String[] args) {
MyThread mt=new MyThread();
new Thread(mt).start();//同一个mt,但是在Thread中就不可以,如果用同一
new Thread(mt).start();//个实例化对象mt,就会出现异常
new Thread(mt).start();
}
};
虽然现在程序中有三个线程,但是一共卖了10张票,也就是说使用Runnable实现多线程可以达到资源共享目的。
Runnable接口和Thread之间的联系:
public class Thread extends Object implements Runnable
发现Thread类也是Runnable接口的子类。
->避免点继承的局限,一个类可以继承多个接口。
->适合于资源的共享
以卖票程序为例,通过Thread类完成:
package org.demo.dff;
class MyThread extends Thread{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣票:ticket"+this.ticket--);
}
}
}
};
下面通过三个线程对象,同时卖票:
package org.demo.dff;
public class ThreadTicket {
public static void main(String[] args) {
MyThread mt1=new MyThread();
MyThread mt2=new MyThread();
MyThread mt3=new MyThread();
mt1.start();//每个线程都各卖了10张,共卖了30张票
mt2.start();//但实际只有10张票,每个线程都卖自己的票
mt3.start();//没有达到资源共享
}
}
如果用Runnable就可以实现资源共享,下面看例子:
package org.demo.runnable;
class MyThread implements Runnable{
private int ticket=10;
public void run(){
for(int i=0;i<20;i++){
if(this.ticket>0){
System.out.println("賣票:ticket"+this.ticket--);
}
}
}
}
package org.demo.runnable;
public class RunnableTicket {
public static void main(String[] args) {
MyThread mt=new MyThread();
new Thread(mt).start();//同一个mt,但是在Thread中就不可以,如果用同一
new Thread(mt).start();//个实例化对象mt,就会出现异常
new Thread(mt).start();
}
};
虽然现在程序中有三个线程,但是一共卖了10张票,也就是说使用Runnable实现多线程可以达到资源共享目的。
Runnable接口和Thread之间的联系:
public class Thread extends Object implements Runnable
发现Thread类也是Runnable接口的子类。
- 在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable,可以实现资源共享
- Java 实现Runnable接口 资源共享
- java多线程------实现Runnable接口创建多进程,实现资源共享
- 多线程实现Runnable接口
- Runnable接口实现多线程
- 多线程实现Runnable接口
- 如果用Runnable就可以实现资源共享[Java多线程]
- 使用Runnable实现资源共享
- 实现Runnable接口实现多线程
- java runnable接口实现多线程
- java多线程实现:Runnable接口
- 实现Runnable,轻松实现多线程间的资源共享
- 通过实现Runnable接口实现多线程
- java 通过实现Runnable接口实现多线程
- 多线程实现方式---实现Runnable接口
- java 用实现Runnable 接口实现多线程
- JAVA 实现Runnable接口实现多线程
- 实现Runnable接口来实现多线程
- 系统调用和库函数的区别
- connect connect
- 谈谈Android里的Context的使用!!!
- Debian x64 编译 32位程序出错的一些解决方法
- mysql [Err] 1005 - Can't create table '.\ \#sql-b34_61.frm' (errno: 150)
- 在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable,可以实现资源共享
- git 教程(7)
- C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem
- windows7启动MySql报错Table 'mysql.plugin' doesn't exist的解决方法
- n人决斗问题
- Cookies 欺骗权限
- poco库学习笔记(5) Poco::Thread
- WCF实战网站
- session和cookie的区别