Java start和run启动线程的区别
我们知道,我们通过调用线程的start方法启动一个线程,那么,我们可以直接调用run方法来启动一个线程吗?
先看下面一段代码:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
运行结果如下:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
或许有人会得出结论,这样启动一个线程是可以的,我们再对程式稍做修改,大家就会发现一个问题:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
这里只在主线程中加入了一行代码,打印一行"Printed by main thread",运行代码,结果如下:
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- Printed by main thread
熟练多线程开发的要发现问题了,为什么"Printed by main thread"会打印在最后一行呢?TestThread类中一直持有时间段吗?
我们对上面的代码进行分析,其实非常简单,这只是一个普通的类中方法的调用,其实是一个单线程的执行,我们来修改代码进一步验证这一点:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.run();
- System.out.println(Thread.currentThread().getName());
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println(Thread.currentThread().getName());
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
这段代码分别在主线程和我们的TestThread的方法中打印当前线程名字,运行结果如下:
- main
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- main
- Printed by main thread
在TestThread类和主线程中运行的是同一个线程,说明在直接调用run时是不能使用多线程的,那么把上面的run方法调用改为start方法的调动再看一下:
- public class Test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TestThread tt = new TestThread();
- tt.start();
- System.out.println(Thread.currentThread().getName());
- System.out.println("Printed by main thread");
- }
- }
- class TestThread extends Thread {
- static int i = 0;
- final static int MAX_I = 10;
- @Override
- public void run() {
- // TODO Auto-generated method stub
- System.out.println(Thread.currentThread().getName());
- while (i < MAX_I) {
- System.out.println(i++);
- }
- }
- }
运行结果如下:
- main
- Thread-0
- 0
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- Printed by main thread
- 9
很明显,这才是我们想看到的结果,所以结论是只有调用Thread的start方法,将线程交由JVM控制,才能产生多线程,而直接调用run方法只是一个普通的单线程程式。
Java start和run启动线程的区别的更多相关文章
- Java Thread 的 run() 与 start() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. ...
- Java并发编程:Java Thread 的 run() 与 start() 的区别
1. sleep 和 wait 方法解释 sleep()方法是Thread类里面的,主要的意义就是让当前线程停止执行,让出cpu给其他的线程,但是不会释放对象锁资源以及监控的状态,当指定的时间到了之后 ...
- java Thread 类 run 和 start 方法区别
public class ThreadModle { public static void main(String[] args) throws InterruptedException { Thre ...
- Java中Thread方法启动线程
public class ThreadTest extends Thread { private int count = 10; @Override public void run() { //重写 ...
- Java Thread 的 sleep() 和 wait() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. sleep ...
- Java线程Run和Start的区别
先上结论:run只是Thread里面的一个普通方法,start是启动线程的方法.何以见得呢?可以执行下面的代码看看run和start的区别: package com.basic.thread; /** ...
- 启动线程,start和run的区别
每个线程都有要执行的任务.线程的任务处理逻辑可以在Tread类的run实例方法中直接实现或通过该方法进行调用,因此 run()相当于线程的任务处理逻辑的入口方法,它由Java虚拟机在运行相应线程时直接 ...
- JAVA面试题 启动线程是start()还是run()?为什么?
面试官:请问启动线程是start()还是run()方法,能谈谈吗? 应聘者:start()方法 当用start()开始一个线程后,线程就进入就绪状态,使线程所代表的虚拟处理机处于可运行状态,这意味着它 ...
- java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器
多线程并发就像是内功,框架都像是外功,内功不足,外功也难得精要. 1.进程和线程的区别 一个程序至少有一个进程,一个进程至少有一个线程. 用工厂来比喻就是,一个工厂可以生产不同种类的产品,操作系统就是 ...
随机推荐
- 【二叉树的递归】07路径组成数字的和【Sum Root to Leaf Numbers】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,节点的值仅限于从0 ...
- 20179215《Linux内核原理与分析》第一周作业
一.Linux介绍 我们现在很常见Windows系统,对于Linux则显得尤为陌生.当然我也不例外,初识Linux过程中遇到一些困惑,但我也在实验的同时通过不断查找资料与实践中慢慢解决问题.那么下面我 ...
- cookie与web Storage
一.cookie 1. http头与cookie cookie是HTTP Cookie的简称.该标准要求: (1)服务器的HTTP响应头包含 Set-Cookie字段 响应头Eg: HTTP/1.1 ...
- HDOJ1151有向图最小路径覆盖
//有向图最小路径覆盖:从某一点出发沿着有向路径,不走回路,能将所有的结点遍历. #include<iostream> #include<cstdio> #include< ...
- 3 K8s安裝ELK+filebeat
1 Filebeat: apiVersion: v1 kind: Service metadata: name: XX spec: ports: - name: http port: targetPo ...
- Ok6410裸机驱动学习(二)ARM基础知识
1.ARM工作模式 ARM微处理器支持7种工作模式,分别为: l 用户模式(usr):ARM处理器正常的程序执行状态(Linux用户态程序) l 快速中断模式(fiq):用于高速数据传输或通道处理 ...
- how to download a file with Nodejs(without using third-party libraries)用node下载文件
创建HTTP GET请求并将其管理response到可写文件流中: var http = require('http'); var fs = require('fs'); var file = fs. ...
- 具体问题:Spring 事务的隔离性,并说说每个隔离性的区别
使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...
- Hive中SELECT TOP N的方法(order by与sort by的区别)
我想说的SELECT TOP N是取最大前N条或者最小前N条. Hive提供了limit关键字,再配合order by可以很容易地实现SELECT TOP N. 但是在Hive中order by只能使 ...
- 在linux系统个人目录下安装新版python
一.下载所需的python版本文件 在linux个人目录下 下载https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tar.xz 解压tar.xz ...