使用Runnable接口实现资源共享的一些心得
2015-05-15 11:54
555 查看
程序的功能就是简单的对一份公共文件实现共享。程序采用的是卖票的形式来演示整个过程。
程序的整体实现
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
Thread t1=new Thread(mt1);
Thread t2=new Thread(mt1);
t1.start();
t2.start();
}
}
程序的结果如下:
因为在程序里面一个对象,启动了两个线程,所以这两个线程一共去卖票。所以出现的结果也是随机的。
但是之前我使用了开启两个对象,然后让他们分别去卖票。程序的代码和效果如下
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
MyThread mt2=new MyThread(“卖票员B”);
Thread t1=new Thread(mt1);
Thread t2=new Thread(mt2);
t1.start();
t2.start();
}
}
此时出现的结果却是:
当然其两个线程却是是开启了的,因为再次运行结果如下:
但是没有实现我的本意:即实现总计票数只有5张。这样Runnable接口实现的功能就类似于继承Thread类,没有实现资源的共享一样。
若是结合上两个程序
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
MyThread mt2=new MyThread(“卖票员B”);
Thread t11=new Thread(mt1);
Thread t12=new Thread(mt1);
Thread t13=new Thread(mt1);
Thread t21=new Thread(mt2);
Thread t22=new Thread(mt2);
Thread t23=new Thread(mt2);
t11.start();
t12.start();
t13.start();
t21.start();
t22.start();
t23.start();
}
}
这样三种情况就表明了:要想实现资源的共享,第一是要使用Runnable接口,第二最能一个对象开启多个线程。这样才能。
程序的整体实现
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
} } }
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
Thread t1=new Thread(mt1);
Thread t2=new Thread(mt1);
t1.start();
t2.start();
}
}
程序的结果如下:
因为在程序里面一个对象,启动了两个线程,所以这两个线程一共去卖票。所以出现的结果也是随机的。
但是之前我使用了开启两个对象,然后让他们分别去卖票。程序的代码和效果如下
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
} } }
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
MyThread mt2=new MyThread(“卖票员B”);
Thread t1=new Thread(mt1);
Thread t2=new Thread(mt2);
t1.start();
t2.start();
}
}
此时出现的结果却是:
当然其两个线程却是是开启了的,因为再次运行结果如下:
但是没有实现我的本意:即实现总计票数只有5张。这样Runnable接口实现的功能就类似于继承Thread类,没有实现资源的共享一样。
若是结合上两个程序
class MyThread implements Runnable{
private String name;
private int ticket=5; //总计五张票
public MyThread(String name){
this.name=name;
}
public void run(){ //覆写Runnable的run()方法
for(int i=1;i<10;i++){
if(ticket>0){//判断是否还有余票
System.out.println(name+”卖票了第”+ticket–+”张票”);
} } }
}
public class RunnableTicketDemo{
public static void main(String args[]){
MyThread mt1=new MyThread(“卖票员A”);
MyThread mt2=new MyThread(“卖票员B”);
Thread t11=new Thread(mt1);
Thread t12=new Thread(mt1);
Thread t13=new Thread(mt1);
Thread t21=new Thread(mt2);
Thread t22=new Thread(mt2);
Thread t23=new Thread(mt2);
t11.start();
t12.start();
t13.start();
t21.start();
t22.start();
t23.start();
}
}
这样三种情况就表明了:要想实现资源的共享,第一是要使用Runnable接口,第二最能一个对象开启多个线程。这样才能。
相关文章推荐
- 使用Runnable接口实现资源共享的一些心得
- Java学习笔记之实现Runnable接口可以资源共享(卖票系统)
- 在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable,可以实现资源共享
- 线程的实现(通过Runnable接口实现资源的共享:卖票)
- Java 实现Runnable接口 资源共享
- 实现Runnable接口适合资源的共享
- java实现Runnable接口适合资源的共享
- java多线程------实现Runnable接口创建多进程,实现资源共享
- 使用Runnable实现资源共享
- 使用Runnable实现数据共享
- 实现Runnable,轻松实现多线程间的资源共享
- 使用Runnable接口实现线程
- 实现Runnable接口的类+使用Thread类的实例来创建线程---->通过实现Runnable接口来创建线程类
- 继承Thread类与实现Runnable接口创建线程对于类实例变量共享的区别
- [原创]java WEB学习笔记55:Struts2学习之路---详解struts2 中 Action,如何访问web 资源,解耦方式(使用 ActionContext,实现 XxxAware 接口),耦合方式(通过ServletActionContext,通过实现 ServletRequestAware, ServletContextAware 等接口的方式)
- 多线程Thread类和Runnable接口资源共享问题分析
- 使用CORS(跨站资源共享Cross-Origin Resource Sharing) 来实现跨域访问
- 并发学习2-使用Thread类来驱动实现Runnable接口的类
- akka-http使用CORS(跨域资源共享)实现跨域请求
- 使用github实现共享代码的一些入门级教程