1.继承Thread类,重载run方法;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
}
});
2.实现Runnable接口,实现run方法;
public class RunnableDemo implements Runnable {

	protected int countDown = 10;
private static int taskCount = 0;
private final int id = taskCount++; public RunnableDemo() {
} public RunnableDemo(int countDown) {
this.countDown = countDown;
} public String status() {
return "#" + id + "(" + (countDown > 0 ? countDown : "LiftOff!") + ").";
} @Override
public void run() {
while (countDown-- > 0) {
System.out.print(status());
Thread.yield();// 将CPU从一个线程转移给另一个线程
}
} public static void main(String[] args) { System.out.println("这个任务的run()不是单独的线程驱动,是在main()中直接调用");
RunnableDemo launch = new RunnableDemo();
launch.run();
System.out.println();
System.out.println("************************************"); System.out.println("在新线程中启动任务");
Thread thread = new Thread(new RunnableDemo());
thread.start();
System.out.println("Waiting for LiftOff");
System.out.println("************************************"); System.out.println("添加多个线程去驱动更多的任务");
for (int i = 0; i < 5; i++) {
new Thread(new RunnableDemo()).start();
}
System.out.println("Waiting for LiftOff"); System.out.println("************************************");
System.out.println("使用executor");
ExecutorService exec = Executors.newCachedThreadPool();
exec=Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
exec.execute(new RunnableDemo());
}
exec.shutdown(); Thread t=new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub }
});
} }

  

【Java】创建线程对象两种方式的更多相关文章

  1. java创建线程的两种方式及源码解析

    创建线程的方式有很多种,下面我们就最基本的两种方式进行说明.主要先介绍使用方式,再从源码角度进行解析. 继承Thread类的方式 实现Runnable接口的方式 这两种方式是最基本的创建线程的方式,其 ...

  2. Java创建线程的两种方式

    方式 继承Thread类 实现Runnable方法 实例 #继承Thread类 public class ThreadTest2 extends Thread { private int thread ...

  3. 当阿里面试官问我:Java创建线程有几种方式?我就知道问题没那么简单

    这是最新的大厂面试系列,还原真实场景,提炼出知识点分享给大家. 点赞再看,养成习惯~ 微信搜索[武哥聊编程],关注这个 Java 菜鸟. 昨天有个小伙伴去阿里面试实习生岗位,面试官问他了一个老生常谈的 ...

  4. 【java并发】传统线程技术中创建线程的两种方式

    传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...

  5. Java并发基础01. 传统线程技术中创建线程的两种方式

    传统的线程技术中有两种创建线程的方式:一是继承Thread类,并重写run()方法:二是实现Runnable接口,覆盖接口中的run()方法,并把Runnable接口的实现扔给Thread.这两种方式 ...

  6. 创建线程的两种方式比较Thread VS Runnable

    1.首先来说说创建线程的两种方式 一种方式是继承Thread类,并重写run()方法 public class MyThread extends Thread{ @Override public vo ...

  7. Java新建线程的两种方式

    Java新建线程有两种方式,一种是通过继承Thread类,一种是实现Runnable接口,下面是新建线程的两种方式. 我们假设有个竞赛,有一个选手A做俯卧撑,一个选手B做仰卧起坐.分别为两个线程: p ...

  8. Java创建线程的四种方式

    Java创建线程的四种方式 1.继承Thread类创建线程 定义Thread类的子类,并重写该类的run方法,run()方法的内容就是该线程执行的内容 创建Thread子类的实例,即创建了线程对象. ...

  9. Java中创建线程的两种方式

    创建线程的第一种方式: 创建一个类继承Thread 重写Thread中的run方法 (创建线程是为了执行任务 任务代码必须有存储位置,run方法就是任务代码的存储位置.) 创建子类对象,其实就是在创建 ...

随机推荐

  1. python2.7中minetypes.py的UnicodeDecodeError

    问题: python错误提示: UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not ...

  2. javascript实现掉落弹出层------Day29

    顾名思义.所谓"掉落弹出层".就是出现一个弹出层,而出现的位置是从上方向下掉落.掉落到指定的位置停止,这样分析起来.和"右下角弹出提醒对话框"比起来,确有异曲同 ...

  3. eclipse properties文件插件

      eclipse properties插件 CreateTime--2018年4月22日22:51:34 Author:Marydon 下载地址:properties文件插件.rar 1.将plug ...

  4. 为什么需要设置pythonpath环境变量?

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #为什么需要设置pythonpath环境变量? #只需设置pythonpath,从而可以从正在用的目录(也就是 ...

  5. RDD转换成DataFrames

    官方提供了2种方法 1.利用反射来推断包含特定类型对象的RDD的schema.这种方法会简化代码并且在你已经知道schema的时候非常适用. 先创建一个bean类 case class Person( ...

  6. VNC-tigervnc-server远程调用图形化

    远程调用Linux图形化,很不错的.. 01.远程Linux须装图形化 yum groupinstall -y    'Desktop'   'X Window System'  #xclock试图形 ...

  7. HDUOJ-----2068RPG的错排

    RPG的错排 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. HDUOJ----专题训练

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Su ...

  9. android适配器Adapter

    一.什么是适配器,适配器有什么用? 适配器是AdapterView视图(如ListView - 列表视图控件.Gallery - 缩略图浏览器控件.GridView - 网格控件.Spinner - ...

  10. navicat 快速复制表所有字段

    查询-新建查询-查询创建工具-选择表-全选字段-查询编辑器