Java中线程的创建有两种方式:
1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在其中
2. 通过实现Runnable接口,实例化Thread类
在实际应用中,我们经常用到多线程,如车站的售票系统,车站的各个售票口相当于各个线程。当我们做这个系统的时候可能会想到两种方式来实现,继承Thread类或实现Runnable接口,现在看一下这两种方式实现的两种结果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
package com.threadtest;
class MyThread extends Thread{
private int ticket = 10 ;
private String name;
public MyThread(String name){
this .name =name;
}
public void run(){
for ( int i = 0 ;i< 500 ;i++){
if ( this .ticket> 0 ){
System.out.println( this .name+ "卖票---->" +( this .ticket--));
}
}
}
}
public class ThreadDemo {
public static void main(String[] args) {
MyThread mt1= new MyThread( "一号窗口" );
MyThread mt2= new MyThread( "二号窗口" );
MyThread mt3= new MyThread( "三号窗口" );
mt1.start();
mt2.start();
mt3.start();
}
}
|
运行结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
一号窗口卖票----> 10
一号窗口卖票----> 9
二号窗口卖票----> 10
一号窗口卖票----> 8
一号窗口卖票----> 7
一号窗口卖票----> 6
三号窗口卖票----> 10
一号窗口卖票----> 5
一号窗口卖票----> 4
一号窗口卖票----> 3
一号窗口卖票----> 2
一号窗口卖票----> 1
二号窗口卖票----> 9
二号窗口卖票----> 8
三号窗口卖票----> 9
三号窗口卖票----> 8
三号窗口卖票----> 7
三号窗口卖票----> 6
三号窗口卖票----> 5
三号窗口卖票----> 4
三号窗口卖票----> 3
三号窗口卖票----> 2
三号窗口卖票----> 1
二号窗口卖票----> 7
二号窗口卖票----> 6
二号窗口卖票----> 5
二号窗口卖票----> 4
二号窗口卖票----> 3
二号窗口卖票----> 2
二号窗口卖票----> 1
|
通过实现Runnable接口的代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package com.threadtest;
class MyThread1 implements Runnable{
private int ticket = 10 ;
private String name;
public void run(){
for ( int i = 0 ;i< 500 ;i++){
if ( this .ticket> 0 ){
System.out.println(Thread.currentThread().getName()+ "卖票---->" +( this .ticket--));
}
}
}
}
public class RunnableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//设计三个线程
MyThread1 mt = new MyThread1();
Thread t1 = new Thread(mt, "一号窗口" );
Thread t2 = new Thread(mt, "二号窗口" );
Thread t3 = new Thread(mt, "三号窗口" );
// MyThread1 mt2 = new MyThread1();
// MyThread1 mt3 = new MyThread1();
t1.start();
t2.start();
t3.start();
}
}
|
运行结果如下:
1
2
3
4
5
6
7
8
9
10
|
一号窗口卖票----> 10
三号窗口卖票----> 9
三号窗口卖票----> 7
三号窗口卖票----> 5
三号窗口卖票----> 4
三号窗口卖票----> 3
三号窗口卖票----> 2
三号窗口卖票----> 1
一号窗口卖票----> 8
二号窗口卖票----> 6
|
为什么会出现这种结果呐。我们不妨做个比喻,其实刚的程序,
继承Thread类的,我们相当于拿出三件事即三个卖票10张的任务分别分给三个窗口,他们各做各的事各卖各的票各完成各的任务,因为MyThread继承Thread类,所以在new MyThread的时候在创建三个对象的同时创建了三个线程;
实现Runnable的, 相当于是拿出一个卖票10张得任务给三个人去共同完成,new MyThread相当于创建一个任务,然后实例化三个Thread,创建三个线程即安排三个窗口去执行。
用图表示如下:
在我们刚接触的时候可能会迷糊继承Thread类和实现Runnable接口实现多线程,其实在接触后我们会发现这完全是两个不同的实现多线程,一个是多个线程分别完成自己的任务,一个是多个线程共同完成一个任务。
其实在实现一个任务用多个线程来做也可以用继承Thread类来实现只是比较麻烦,一般我们用实现Runnable接口来实现,简洁明了。
大多数情况下,如果只想重写 run() 方法,而不重写其他 Thread 方法,那么应使用 Runnable 接口。这很重要,因为除非程序员打算修改或增强类的基本行为,否则不应为该类(Thread)创建子类。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://mars914.iteye.com/blog/1508429
相关文章
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-29 56
-
2025-05-25 67
-
2025-05-27 57
-
2025-05-29 55
-
2025-05-24 29