Java線程start()vs run()方法及示例

Java | 線程start()vs run()方法 (Java | Thread start() vs run() Methods)

When we call the start() method, it leads to the creation of a new thread. Then, it automatically calls the run() method. If we directly call the run() method, then no new thread will be created. The run() method will be executed on the current thread only.

當我們調用start()方法時 ,它將導致創建新線程。 然后,它會自動調用run()方法 。 如果我們直接調用run()方法,則不會創建新線程。 run()方法將僅在當前線程上執行。

That is why we have the capability of calling the run method multiple times as it is just like any other method we create. However, the start() method can only be called only once.

這就是為什么我們能夠多次調用run方法的原因,就像我們創建的任何其他方法一樣。 但是,start()方法只能被調用一次。

Consider the following code:

考慮以下代碼:

class MyThread extends Thread { 
public void run() 
{ 
System.out.println("Thread Running: " + Thread.currentThread().getName()); 
} 
} 
public class MyProg { 
public static void main(String[] args) 
{ 
MyThread t1 = new MyThread(); 
t1.start(); 
} 
} 

Output

輸出量

Thread Running: Thread-0

We can clearly see that the run() method is called on a new thread, default thread being named Thread-0.

我們可以清楚地看到, run()方法是在新線程上調用的,默認線程名為Thread-0

Consider the following code:

考慮以下代碼:

class MyThread extends Thread { 
public void run() 
{ 
System.out.println("Thread Running: " + Thread.currentThread().getName()); 
} 
} 
public class MyProg { 
public static void main(String[] args) 
{ 
MyThread t1 = new MyThread(); 
t1.run(); 
} 
}

Output

輸出量

Thread Running: main

We can see that in this case, we call the run() method directly. It gets called on the current running thread -main and no new thread is created.

我們可以看到在這種情況下,我們直接調用run()方法 。 在當前正在運行的線程-main上調用它,并且不創建新線程。

Similarly, in the following code, we realize that we can call the start method only once. However, the run method can be called multiple times as it will be treated as a normal function call.

同樣,在下面的代碼中,我們意識到只能調用一次start方法。 但是,可以多次調用run方法,因為它將被視為常規函數調用。

class MyThread extends Thread { 
public void run() 
{ 
System.out.println("Thread Running: " + Thread.currentThread().getName()); 
} 
} 
public class MyProg { 
public static void main(String[] args) 
{ 
MyThread t1 = new MyThread(); 
t1.run();
t1.run();
t1.start();
t1.start();
} 
} 

Output

輸出量

Thread Running: main
Thread Running: main
Thread Running: Thread-0
Exception in thread "main" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.start(Thread.java:794)

翻譯自: https://www.includehelp.com/java/thread-start-vs-run-methods-with-examples.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541724.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541724.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541724.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux安裝卸載mysql,Linux6 系列 安裝、卸載mysql

Linux6 系列 安裝、卸載mysqlLinux6 系列 安裝、卸載mysqlLinux環境下載mysql:https://blog.csdn.net/weixin_40816738/article/details/90111456一、安裝環境依賴:yum install -y cmake make gcc gcc-c libaio ncurses ncurses-devel二、安裝流程1、軟件…

Python | 如何使用pip升級所有Python軟件包?

While using Python as a programming language, its a very common scenario to use a virtual environment and PIP, a package manager for python. 當使用Python作為編程語言時,使用虛擬環境和PIP (Python的程序包管理器)是一種非常常見的情況。 Its a common …

linux下enum類型占幾個字節,enum大小問題

問題描述板卡有兩個CPU,ARMMIPS,同時運行三個系統REE(linux) TEE(SierraTEE) SEE(TDS)。TEE跟SEE通過RPC進行通信,有enum成員的結構體信息傳遞會出錯,如下結構體:struct sTag {enum A;enum B;int C;enum D;};問題分析…

ASP.NET導出word實例

ASP.NET導出word實例 最近遇到一個題目就是如何在asp.net中將數據導出到word中,由于數據是動態的,所以需要在后臺拼出想要的的格式,翻遍了網頁找出了一個比較滿意的代碼,感謝那位高手。代碼如下: public void Download…

Java LocalDate類| toString()方法與示例

LocalDate類toString()方法 (LocalDate Class toString() method) toString() method is available in java.time package. toString()方法在java.time包中可用。 toString() method is used to represent this LocalDate as a String by using the standards ISO-8601 format.…

linux14.04 Apache,Ubuntu 14.04編譯安裝Apache

Ubuntu下編譯安裝apache需要預先編譯安裝多個依賴件,包括:apr, apr-util,pcre,zlib-devel,等,相當麻煩,記錄于此備查.由于Ubuntu系統默認安裝時沒有安裝C,所以也需要先安裝c編譯需要相關的組件。[注]apt-ca…

Android Jenkins自動化構建之路

install Jenkins 添加Jenkins的源(repository): sudo wget -O /etc/yum.repos.d/jenkins.repo http://jenkins-ci.org/redhat/jenkins.repo sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key復制代碼yum install Jenkins復制代碼…

java 根據類名示例化類_Java即時類| plusMillis()方法與示例

java 根據類名示例化類即時類plusMillis()方法 (Instant Class plusMillis() method) plusMillis() method is available in java.time package. plusMillis()方法在java.time包中可用。 plusMillis() method is used to add the given duration in milliseconds to this Insta…

linux dd入門,Linux基礎知識:Linux中DD命令詳解

1.dd命令簡介功能:把指定的輸入文件拷貝到指定的輸出文件中,并且在拷貝過程中可以進行格式轉換。可以用該命令實現DOS下的diskcopy命令的作用。先用dd命令把軟盤上的數據寫成硬盤的一個寄存文件,再把這個寄存文件寫入第二張軟盤上&#xff0c…

CSS 字體(font)實例

1、設置文本字體 font-family:"Times New Roman",Georgia,Serif font-family:Arial,Verdana,Sans-serif 2、設置字體尺寸 font-size: 100% 3、設置字體風格 font-style:normal font-style:italic font-style:oblique 4、設置字體的異體 font-variant:normal text-var…

Java Duration類| 帶示例的compareTo()方法

持續時間類compareTo()方法 (Duration Class compareTo() method) compareTo() method is available in java.time package. compareTo()方法在java.time包中可用。 compareTo() method is used to compare this Duration object to the given object. compareTo()方法用于將此…

linux定時任務執行url,科技常識:linux定時任務訪問url實例

今天小編跟大家講解下有關linux定時任務訪問url實例 ,相信小伙伴們對這個話題應該也很關注吧,小編也收集到了有關linux定時任務訪問url實例 的相關資料,希望小伙伴會喜歡也能夠幫助大家。這次linux定時任務設置成功,也算是自己學習…

lcase和ucase_在SQL中使用UCASE(),LCASE()和MID()函數

lcase和ucaseUpper Case, Lower Case and MID functions are scalar functions which return a single value, based in the input value. 大寫,小寫和MID函數是標量函數,它們基于輸入值返回單個值。 As you all know sometimes different databases ha…

Maven的Settings.xml配置文件解釋

該配置用于單用戶配置和全局配置, 單用戶配置默認存放于 ${user.home}/.m2/目錄中. 全局配置默認存放于Maven安裝目錄下面的conf目錄中. 這兩個默認的位置都可以修改. <?xml version"1.0" encoding"UTF-8"?> <settings xmlns"http://m…

linux ntp手動授時,關于我校NTP授時服務的使用說明

校園網用戶&#xff1a;我中心于近期采購了GPS北斗授時服務設備&#xff0c;該設備可實現純GPS模式、純北斗模式和混合模式與衛星對時&#xff0c;同時實現對校內設備授時的功能。支持所有NTP協議的服務器、PC、嵌入式設備等&#xff0c;包括但不限于&#xff1a;Microsoft Win…

一串字符串轉換為ascii_將ASCII字符串(char [])轉換為C中的BYTE數組

一串字符串轉換為asciiGiven an ASCII string (char[]) and we have to convert it into BYTE array (BYTE[]) in C. 給定一個ASCII字符串(char [])&#xff0c;我們必須將其轉換為C語言中的BYTE數組(BYTE [])。 Logic: 邏輯&#xff1a; To convert an ASCII string to BYTE…

debugging Auto Layout:Logical Errors

Logical Errors邏輯錯誤 Logical errors are simply bugs. Somewhere, you have an assumption that is faulty. Perhaps it’s an assumption about how Auto Layout calculates the views’ frames. Perhaps it’s an assumption about the set of constraints that you’ve …

linux反序列化漏洞,思科多個產品Java反序列化漏洞(CVE-2015-6420)

思科多個產品Java反序列化漏洞(CVE-2015-6420)發布日期&#xff1a;2015-12-15更新日期&#xff1a;2015-12-17受影響系統&#xff1a;Cisco Unified ComputingCisco Voice and Unified Communications DevicesCisco Wireless描述&#xff1a;CVE(CAN) ID: CVE-2015-6420思科是…

密碼學替代技術_替代技術及其類型| 密碼學

密碼學替代技術As we already discussed what are the Substitution techniques and one of its type Ceasar Cipher? So we are not discussing it here for that please refer to Cryptography: CeasarCipher here: Cryptography: Caesar Cipher and its Python Implementat…

Flask+uwsgi+Nginx環境搭建

2019獨角獸企業重金招聘Python工程師標準>>> 開源軟件準備 需要的軟件列表&#xff1a; setuptools-33.1.1.zip Python-2.7.13.tgz pip-9.0.1.tar.gz nginx-1.10.3.tar.gz 軟件統一上傳到/usr/local/src/下&#xff0c;python是使用自己編譯的。Python安裝 先安裝以…