Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别
2024-10-15 16:19:05
进程和线程的区别:
进程是重量级的任务,需要分配给它们独立的地址空间。进程间通信是昂贵和受限的。进程间的转换也是很需要花费的。
另一方面,线程是轻量级的选手。它们共享地址空间并且共享同一个进程。线程间通信是便宜的,线程间的转换也是低成本的。
线程的生命周期
一个线程从创建到消亡的过程。线程的生命周期分为四个状态
1、创建状态
2、可运行状态
3、不可运行状态
4、消亡状态
线程的状态转换图
Java创建线程的三种方式: 1 继承Thread, 2.实现Runnable接口 3、实现Callable接口
一、Java 使用线程方式Thread和Runnable,以及Thread与Runnable的区别
<一>. java中实现线程的方式有Thread和Runnable
Thread:
public class Thread1 extends Thread{
@Override
public void run() {
System.out.println("extend thread");
}
}
Runnable:
public class ThreadRunable implements Runnable{ public void run() {
System.out.println("runbale interfance"); } }
使用
public static void main(String[] args) {
new Thread1().start();
new Thread(new ThreadRunable()).start();
}
<二>.Thread和Runnable区别
1.在程序开发中使用多线程实现Runnable接口为主。 Runnable避免继承的局限,一个类可以继承多个接口
2. 适合于资源的共享
如下面的例子
public class TicketThread extends Thread{ private int ticketnum = 10; @Override
public void run() {
for(int i=0; i<20;i++){
if (this.ticketnum > 0) {
ticketnum--;
System.out.println("总共10张,卖掉1张,剩余" + ticketnum);
}
}
}
}
使用三个线程
public static void main(String[] args) {
TicketThread tt1 = new TicketThread();
TicketThread tt2 = new TicketThread();
TicketThread tt3 = new TicketThread();
tt1.start();
tt2.start();
tt3.start();
}
实际上是卖掉了30张车票
而使用Runnable,如下面的例子
public class TicketRunnableThread implements Runnable{ private int ticketnum = 10; public void run() {
for(int i=0; i<20;i++){
if (this.ticketnum > 0) {
ticketnum--;
System.out.println("总共10张,卖掉1张,剩余" + ticketnum);
}
}
}
}
使用三个线程调用
public static void main(String[] args) {
TicketRunnableThread trt1 = new TicketRunnableThread();
new Thread(trt1).start();
new Thread(trt1).start();
new Thread(trt1).start();
}
因为TicketRunnableThread是New了一次,使用的是同一个TicketRunnableThread,可以达到资源的共享。最终只卖出10张车票。
3.效率对比
public static void main(String[] args) { long l1 = System.currentTimeMillis(); for(int i = 0;i<100000;i++){
Thread t = new Thread();
} long l2 = System.currentTimeMillis(); for(int i = 0;i<100000;i++){
Runnable r = new Runnable() {
public void run() {
}
};
} long l3 = System.currentTimeMillis(); System.out.println(l2 -l1);
System.out.println(l3 -l2);
}
在PC上的结果
119
5
所以在使用Java线程池的时候,可以节约很多的创建时间
二、线程池
public class MyCallable implements Callable<Object> { private String taskNum; MyCallable(String taskNum){
this.taskNum = taskNum;
} @Override
public Object call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
Thread.sleep(1000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(">>>" + taskNum + "任务终止");
return taskNum +"任务返回运行结果,当前任务执行时间[" + time + "]毫秒";
} public static void main(String[] args) throws ExecutionException, InterruptedException {
int taskSize = 5;
//创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
//创建多个有返回值的任务
List<Future> list = new ArrayList<>();
for(int i = 0; i< taskSize; i++){
Callable c = new MyCallable(i+ " ");
//执行任务并获取Future对象
Future f = pool.submit(c);
list.add(f);
}
pool.shutdown(); for(Future f : list){
//从Future对象获取任务的返回值
System.out.println(">>>" + f.get().toString());
} }
}
最新文章
- IE11部分图片无法显示的兼容办法
- Exception Handling in ASP.NET Web API webapi异常处理
- [ASE]Sprint1总结 &; Sprint2计划
- magento做手机端思路
- ipv4、ipv6的socket同时监听“bind: Address already in use”的解决方法
- java基础之synchronized使用方法
- Event — Windows API
- 设置cas server使用http非安全协议
- SqlServer sysobjects_table
- 第七十九,CSS3背景渐变效果
- 自己手写WEB程序框架并执行
- js学习之原生js实现懒加载
- Openvswitch手册(3): sFlow, netFlow
- Spring.NET依赖注入框架学习--入门
- C# 编写WCF简单的服务端与客户端
- Python ————反射机制
- javascript sourcemap
- Linux内核分析——计算机是如何工作的
- 转:初探nginx架构(一)
- Eclipse删除代码中所有注释及空格
热门文章
- 第K大数
- query语句的拼接.
- UE4 WCF RestFul 服务器 读取JSON 数据并解析 简单实例
- JAVAWEB学习总结 SERVLET开发(二)
- [转]sed命令详解
- Android Studio Problem : failed to find style &#39;textviewstyle&#39; in current theme 解决方法
- Java(一)
- 信号量与PV操作
- 关于调用deleteRowsAtIndexPaths withRowAnimation方法出现错误
- C# 属性控件2