java实现线程的方法
来源:互联网 发布:螺栓连接计算软件 编辑:程序博客网 时间:2024/06/03 06:17
1. 继承Thread
Thread thread1 = new Thread(){ @Override public void run() { System.out.println("子线程"+new Date().getTime()); } }; thread1.start(); System.out.println("结束!");// 结束!// 子线程15112458599862.实现Runnable接口
Runnable runnable = new Runnable() { @Override public void run() { System.out.println("子线程:"+new Date().getTime()); } }; new Thread(runnable).start(); System.out.println("结束!!!");3. 实现Callable
public class MyCallable implements Callable<Long> { @Override public Long call() throws Exception { Thread.sleep(2000); return new Date().getTime(); }}public void test1(){ ExecutorService pool = Executors.newFixedThreadPool(15); List<Future<Long>> results = new ArrayList<Future<Long>>(); MyCallable myCallable = new MyCallable(); for(int i=0;i<12;i++ ){ results.add(pool.submit(myCallable)); } pool.shutdown(); for(Future future:results){ try { Long times = (Long) future.get(); Date date = new Date(times); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(" "+ simpleDateFormat.format(date)); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }1)
阅读全文
0 0
- java实现线程的方法
- java中线程安全的实现方法
- JAVA实现线程的三种方法
- Java线程安全的实现方法
- Java线程的两种实现方法
- java实现线程同步的方法
- java 线程池的四实现方法
- java两种实现线程的方法
- JAVA线程之一:实现一个线程的两种方法
- 实现线程的方法
- 线程实现的方法
- 线程的实现方法
- [Java]Java实现线程操作的两种方法
- Java线程简单实现方法
- Java线程同步实现方法
- JAVA线程-线程的实现
- JAVA的线程对象实现的两种方法
- 在Java中实现线程的两种方法
- 5. ROS编程入门--航迹推算(dead reckoning)
- linux centos安装RabbitMQ
- 双十一/黑五剁完手后,卖家不发货咋办?
- Mysql基础知识
- ROS笔记--主题发布节点和主题订阅节点
- java实现线程的方法
- 模板引擎artTemplate 介绍
- Vue2.0+Vue-cli脚手架
- ubuntu16常用操作命令
- 安卓 版本更新
- 粘贴后出发事件 ueditor
- 重载new()delete()的笔记----C++学习之路
- 浅谈optimizer_mode优化器模式
- 制作pdf文档书签,自动生成or根据目录生成