Java并发编程:线程的创建

*/-->

code {color: #FF0000}
pre.src {background-color: #002b36; color: #839496;}

Java并发编程:线程的创建

在Java中线程的创建主要有两种,一种是通过继承抽象类Thread,一种是通过实现Runnable接口。当然,还有Concurent包里面的Callable和Future也可以算是一种。

1 Thread

我们先来看一下,使用Thread如何创建线程:

public class ThreadDemo extends Thread {
@Override
public void run() {
System.out.println("Current thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.start();
}
}
}
Current thread name is: Thread-0
Current thread name is: Thread-2
Current thread name is: Thread-1
Current thread name is: Thread-3
Current thread name is: Thread-5
Current thread name is: Thread-4
Current thread name is: Thread-6
Current thread name is: Thread-7
Current thread name is: Thread-8
Current thread name is: Thread-9

可以看到,我们创建来10个线程,但是执行的顺序是不确定的。可以设置优先级,默认是5,最大是10,最小是1.但是即使设置来优先级,顺序也是不能保证。

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new ThreadDemo();
thread.setPriority(i + 1);
thread.start();
}
}

优先级逐渐上升,但执行但结果还是一样。

2 Runnable

我们再来使用Runnable创建线程:

public class RunnableDemo implements Runnable {
@Override
public void run() {
System.out.println("Current runnable thread name is: " + Thread.currentThread().getName());
} public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.start();
}
}
}
Current runnable thread name is: Thread-0
Current runnable thread name is: Thread-3
Current runnable thread name is: Thread-2
Current runnable thread name is: Thread-5
Current runnable thread name is: Thread-1
Current runnable thread name is: Thread-6
Current runnable thread name is: Thread-7
Current runnable thread name is: Thread-4
Current runnable thread name is: Thread-8
Current runnable thread name is: Thread-9

可以看出,实现Runnable接口接口之后但RunnableDemo类但实例还是无法直接运行的,它必须将实例对象传入Thread类,然后,才能调用Thread对象中的start()进行启动。

3 start() 和 run()

我们接下来来看一下start() 和 run()的区别:
当我们中定义新的线程类的时候,唯一覆写的方法就是run()。那么,我们能不能直接调用run()方法呢?
答案是肯定的。
我们将start()替换为run()再试一下:

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
Thread thread = new Thread(new RunnableDemo());
thread.run();
}
}
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main
Current runnable thread name is: main

结果全是主线程在运行,也就是说根据就没有新的线程启动运行。所以,使用run()方法调用时,就和一般的函数调用一样,是由当前线程进行调用的,并不会启动新的线程,然后在新的线程中运行这个run()方法。只有使用start()方法去调用,才会启动新的线程,然后,在新的线程中运行run()方法。

Date: 2017-07-04 21:37

Author: WEN YANG

Created: 2017-07-04 Tue 22:44

Emacs 25.2.1 (Org mode 8.2.10)

Validate

Java并发编程:线程的创建的更多相关文章

  1. Java并发编程:如何创建线程?

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  2. Java并发编程:线程和进程的创建(转)

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  3. 【转】Java并发编程:如何创建线程?

    一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也有地方称为JVM进程),一般来说名字默认是java.exe或者javaw.exe(windows下可以通过 ...

  4. 2、Java并发编程:如何创建线程

    Java并发编程:如何创建线程? 在前面一篇文章中已经讲述了在进程和线程的由来,今天就来讲一下在Java中如何创建线程,让线程去执行一个子任务.下面先讲述一下Java中的应用程序和进程相关的概念知识, ...

  5. Java 并发编程 | 线程池详解

    原文: https://chenmingyu.top/concurrent-threadpool/ 线程池 线程池用来处理异步任务或者并发执行的任务 优点: 重复利用已创建的线程,减少创建和销毁线程造 ...

  6. java并发编程 线程基础

    java并发编程 线程基础 1. java中的多线程 java是天生多线程的,可以通过启动一个main方法,查看main方法启动的同时有多少线程同时启动 public class OnlyMain { ...

  7. java并发编程 | 线程详解

    个人网站:https://chenmingyu.top/concurrent-thread/ 进程与线程 进程:操作系统在运行一个程序的时候就会为其创建一个进程(比如一个java程序),进程是资源分配 ...

  8. Java并发编程:线程间通信wait、notify

    Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...

  9. Java并发编程——线程的基本概念和创建

    一.线程的基本概念: 1.什么是进程.什么是是线程.多线程? 进程:一个正在运行的程序(程序进入内存运行就变成了一个进程).比如QQ程序就是一个进程. 线程:线程是进程中的一个执行单元,负责当前进程中 ...

  10. Java并发编程——线程池的使用

    在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统 ...

随机推荐

  1. 微信小程序点击保存图片到相册

    wxml部分的代码   <view class="footer-r" bindtap="save"> <image src="../ ...

  2. Python之路-条件控制&循环语句&列表推导式&常用函数

    一.什么是条件控制语句 条件控制语句,也可以称之为判断语句,通过一条或多条的执行结果来决定接下来要执行的代码块. 二.if语句 if语句是用来进行判断的,最简答的if语句只有一个判断一个操作. 语法: ...

  3. 在eclipse里搜索maven项目需要的dependency

    eclipse直接就可以通过下载同步仓库索引,直接关键字查询需要的dependency. 前提是你已经在你的eclipse上配好了maven正确的环境. 1. 设置在开启eclipse时下载同步仓库索 ...

  4. 【改】DOS文件格式转UNIX文件格式

    windows中的文本文件的换行符是"\r\n",而linux中是"\n",dos格式文件传输到unix系统时,会在每行的结尾多一个^M,当然也有可能看不到,但 ...

  5. Java List 类型转换

    java的类型转换让人忍不住在开始编码前骂一声  FUCK! 1  子类转父类 up List<SubClass> subs = ...; List<? extends BaseCl ...

  6. ps:不规则选区

    尽管我们学会了如何添加减去或是交叉选区,但选取出来的选区还是比较规则,不是矩形就是圆形,这样的形状很难胜任在实际制作中的需要.现在我们就要学习如何建立一个任意形状的选区.建立任意选区的工具是套索工具. ...

  7. ssm框架整合抽取BaseDao接口

    import java.io.Serializable; import java.util.List; /** * DAO基础操作模板 * * @param <T> 泛型 */ publi ...

  8. count(*),count(1),count(列名)的区别

    count(*)和count(1)无任何差别,永远优于count其他字段只要存在普通索引,count就会使用普通索引,只存在主键时,count(*)和或count(1)会使用主键索引 count(a) ...

  9. 运行 tensorboard

    使用下面命令总是报错: tensorboard --logdir=mylogdir tensorboard --logdir='./mylogdir' 正确命令 tensorboard --logdi ...

  10. dubbo、web应用 项目结构以及发布目录结构

    一.dubbo服务项目结构及发布结构 dubbo 服务项目结构 xxxxx-api 接口类和一些DTO 用于供其他项目依赖 需要提供dubbo服务的接口命名 以Facade结尾 (xxxxxFacad ...