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. 你的package包名有问题!

    今天在Eclipse中运行我的Java程序中,就弹出了以下消息的窗口: 注意窗口的名字为Java Virtual Machine Launcher . Error : A JNI error has ...

  2. Taro -- 上传图片公用组件

    Taro上传图片公用组件 子组件chooseImage //component/chooseImage/index.js import Taro, { Component } from '@taroj ...

  3. C#基础提升系列——C#任务和并行编程

    C#任务和并行编程 我们在处理有些需要等待的操作时,例如,文件读取.数据库或网络访问等,这些都需要一定的时间,我们可以使用多线程,不需要让用户一直等待这些任务的完成,就可以同时执行其他的一些操作.即使 ...

  4. js点击获取—通过JS获取图片的相对坐标位置

    一.通过JS获取鼠标点击时图片的相对坐标位置 源代码如下所示: <!DOCTYPE html> <html lang="en"> <head> ...

  5. 打印XX提交的svn版本信息

    打印出匹配uliuchao或--结尾的行 svn log | sed -n '/uliuchao/,/--$/p'

  6. Flask学习 3 url_for的使用

    #!/usr/bin/env python # encoding: utf-8 """ @version: v1.0 @author: cxa @file: flask0 ...

  7. HTML5 新属性的讲解

    1.选择器: 标签选择器: class选择器: id选择器: 后代选择器:div li div下所有li 子代选择器:div>li div的所有子一代 li 元素 交集选择器:div.class ...

  8. 生产环境下,oracle不同用户间的数据迁移。第二部分

    任务名称:生产环境下schema ELON数据迁移至schema TIAN######################################## 测试二:测试参数remap_tablespa ...

  9. vue鼠标修饰符

    鼠标左键事件 <div @click.left="mouseClick" style="border: solid 1px red; width:500px; he ...

  10. 牛客提高D5t1 deco的abs

    分析 傻子题? 对d取模后随便贪心即可 代码 #include<iostream> #include<cstdio> #include<cstring> #incl ...