博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thread’s start method and run method
阅读量:7251 次
发布时间:2019-06-29

本文共 3110 字,大约阅读时间需要 10 分钟。

工作中用到了Thread,一开始用错了,仔细研究了一下,稍作整理。

前言,今天写代码居然这样写的

 

new Thread() { 	@Override	public void run() {		System.out.println("test");	} }.run();

 

天真得以为这样这样会新开启一个线程执行,可是打印了线程的信息之后,发现还是在主线程中。仔细一想,确实我这樣做和调用一个新的Object的toString方法有区别么,根本不会产生神马新的线程。
当然我们的主要实现是在run里面,这也是Thread唯一implements Runnable的一个方法。
于是我们调用start方法,可以在新线程中执行run中的处理逻辑,于是会一位start方法调用了run方法
然后却又不是这样
start的方法代码

 

/**     * Starts the new Thread of execution. The run() method of     * the receiver will be called by the receiver Thread itself (and not the     * Thread calling start()).     *     * @throws IllegalThreadStateException if the Thread has been started before     *     * @see Thread#run     */    public synchronized void start() {        if (hasBeenStarted) {            throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?        }         hasBeenStarted = true;         VMThread.create(this, stackSize);    }

 

 

请注意上面的javadoc The code run() method of the receiver will be called by the receiver Thread itself (and not the Thread calling start() )

而究竟是谁这么隐蔽地调用run方法呢
其实是start间接地调用,start调用VMThread的create方法,而系统(OS创建需要的线程),然后JVM调用run方法。直接调用的是jvm。
下面是一篇很geek的文章讲解:

start method

public void start() – this method is responsible for causing the thread (it’s called upon) to begin execution. The name may seem to be a misnomer here as this method only causes and not actually starts the execution. It just schedules the thread and when the CPU Scheduler picks this thread for excution then the JVM calls the run() method to actually start the execution of the thread.

This method will obviously result into two concurrent threads – one, from which this method is called and two, the new thread which executes the run() method of the new Thread instance.

A thread will throw IllegalStateException in case you try to call the start() method on an already started thread instance. That means, a thread can not be started more than once. As per the Java specifications a thread may not be restarted after completing its execution. You’ll be required to create and start the execution of a new thread in that case.

run method

public void run() – this is the only method of the Runnable interface and the classes which intend to execute their code in a separate thread of execution first implement the Runnable interface and subsequently define this method and put all the code expected to be executed in the separate thread inside the scope of this run method.

The other way, such classes can follow for the same is by extending the Thread class and in that case also they should oevrride the run method of the Thread class and put all the code supposed to be executed in the new thread inside the scope of the overriden run method.

Difference between start() and run() methods

start() methods only schedules the thread for execution and not actually begins the execution of the thread. The execution of the thread is started when the JVM calls the run() method of the thread once the CPU Scheduler picks this scheduled thread for execution.

文章来源:http://geekexplains.blogspot.com/2008/05/start-method-vs-run-method-in-java.html

 
 

 

 

转载地址:http://xvhbm.baihongyu.com/

你可能感兴趣的文章
TCExam文件代码注释分析(后台首页admin/code/index.php)
查看>>
Finereport在企业级BI分析中的应用
查看>>
linux内核参数注释与优化
查看>>
linux 2.6x内核升级
查看>>
pxe
查看>>
NFS网络文件系统安装
查看>>
网页嵌入自动生成当前网页二维码图片代码
查看>>
Linux时间同步服务
查看>>
Python基础-----列表、元组、集合(2)
查看>>
iptables详解
查看>>
Redisson官方文档 - 12. 独立节点模式
查看>>
AD域笔记
查看>>
HTTP协议详解
查看>>
apache实现多端囗多域名配置
查看>>
Linux命令(15):type命令
查看>>
第一单元作业
查看>>
Azure云端部署Exchange 2016双数据中心—Part6(DAG切换测试)
查看>>
通过ansible部署高可用LNAMMKP架构
查看>>
IBM Aix系统添加硬盘步骤
查看>>
“esxcli software vib” commands to patch an ESXi 5.x/6.x host (2008939)
查看>>