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. django 项目创建使用

    1. web框架的本质: socket服务端 与 浏览器的通信 2. socket服务端功能划分: a. 负责与浏览器收发消息(socket通信) --> wsgiref/uWsgi/gunic ...

  2. 记录cobbler报错

    出现下面这个错误解决方法 httpd does not appear to be running and proxying cobbler, or SELinux is in the way. Ori ...

  3. Car的旅行路线(Floyd+模拟)

    题目地址 贼鸡儿猥琐的一道题 好在数据不毒瘤,而且Floyd就OK了. 这道题的难点在于 建图,也很考验模拟能力,需要十分的有耐心. 建图 题目中告诉了我们一个矩形的三个点 我们在平面直角坐标系中随便 ...

  4. bzoj4773 负环 倍增+矩阵

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4773 题解 最小的负环的长度,等价于最小的 \(len\) 使得存在一条从点 \(i\) 到自 ...

  5. css--图片整合(精灵图)

    图片整合(精灵图) 精灵图的优点: 减少图片的字节 减少了网页的http请求,从而大大的提高了页面的性能 解决了网页设计师在图片命名上的困扰,只需对一张集合的图片上命名就可以了,不需要对每一个小元素进 ...

  6. Git的配置与基本操作

    Git是一个版本控制软件,它可以让我们能够拍摄处于可行状态的项目的快照,修改项目(如实现新功能)后,如果项目不能正常运行,可以恢复到前一个可行状态. 通过使用版本控制,我们可以无忧无虑的改进项目,不用 ...

  7. 029:url标签使用详解

    url标签使用详解: 在模版中,我们经常要写一些 url ,比如某个 a 标签中需要定义 href 属性.当然如果通过硬编码的方式直接将这个 url 写死在里面也是可以的.但是这样对于以后项目维护可能 ...

  8. 2018年第九届山东省ACM省赛总结

    去年打完区域赛之后,面对着两个队友都去找实习的情况,我自己对今年省赛还是有点慌的.不只一次的像我的队友说明自己很慌,但是老曹跟会长都说:“没事,慌啥!”前几场训练赛因为老曹跟秋洁有面试有时候只能一个人 ...

  9. 黑苹果 MacOS 10.15 Catalina安装教程

    10.15 Catalina 桌面 一.准备工作 一个8G以上的U盘(有的U盘标的是8G,实际只有7.X,实际容量小于7.5G的会失败) MacOS镜像.TransMac(刻录工具).DiskGeni ...

  10. Linux负载均衡软件LVS

    linux下的开源负载均衡软件LVS的安装.配置和使用.LVS是一个中国人创建和开发的开放源码项目,利用LVS可以构建高可用.高可靠的负载均衡集群,因此,利用Linux+LVS不但可以假设高性能的负载 ...