之前放在自己网站上的例子,因为网站关闭,已经找不到了,想用的时候,没有的话又重新翻书是很麻烦的事情。所以重新记录一下,以备将来查看。

第一种,让任务类继承Runable接口,然后将任务类对象放入Thread对象的构造器,通过Thread.start()方法执行调用。

package com.test.thread;

public class ThreadTest1 {
public static void main(String[] args) {
Thread thread = new Thread(new task(1));
thread.start();
    System.out.println("hello");
}
} class task implements Runnable { private final int taskid; public task(int taskid) {
this.taskid = taskid;
} public void run() {
System.out.println(this);
} @Override
public String toString() {
return "task [taskid=" + taskid + "]";
} }

执行结果:

hello
task [taskid=1]

第二种,就是第一种方式的改进,好处之一就是不用显示地创建Thread对象了,其他的好处还有,我不知道。这是JAVA SE5/6启动任务的优选方法。和方式一的区别,仅仅是main方法的区别:

ExecutorService exec = Executors.newCachedThreadPool();
exec.execute(new Task1(1));  

还有一种就是继承Thread类啦。并发编程太复杂了, 暂时写这么多把,以后用到的时候再说。

最新文章

  1. JDBC关于时间的存取
  2. iOS,视图相关
  3. div基础
  4. ZPL打印中文信息
  5. A + B Problem II
  6. [Effective JavaScript 笔记]第20条:使用call方法自定义接收者来调用方法
  7. stuts-security.xml
  8. (转)IOS中获取各种文件的目录路径的方法
  9. [RxJS] Error handling operator: catch
  10. 内存泄露 Memory Leaks
  11. js-轮播图
  12. BCB实现BMP图片的RGB分解(转)
  13. coreCLR系列随笔 之ClrJit项目之alloc.cpp文件分析(1)
  14. java设计模式--单列模式
  15. python 之 初识模块
  16. 你有可能不知道的css浮动问题
  17. [OC] 使用 cocoaPods 导入 AFNetworking
  18. pytorch-1.0 踩坑记录
  19. jenkins构建触发器详解-不登录触发远程构建
  20. Save vtkMatrix4x4 to a file 保存到文件

热门文章

  1. 个人小应用服务器安装搭建,HP 360p Gen9 使用winpe安装centos[一]
  2. 【CF1187C】Vasya And Array
  3. K8S概念
  4. dlerror和dlclose用法
  5. css-动画,a标签下,文字加下划线,而且动画是由中间向两边扩展开
  6. MHA配置
  7. CGI中使用Cookie
  8. sh_03_列表的数据统计
  9. unittest详解(四) 批量执行用例(discover)
  10. sublime text 3设置