scala 多線程_Scala中的多線程

scala 多線程

Scala多線程 (Scala Multithreading)

Multithreading is the concept of using multiple threads simultaneously that allows the program to perform multiple operations simultaneously.

多線程是同時使用多個線程的概念,它允許程序同時執行多個操作。

A thread is a lightweight sub-processes that require a very lesser amount of memory to get executed. The general multithreaded program consists of two or more thread that runs concurrently to optimize the use of resources ( multiple CPUs) and increase the performance of the program.

線程是輕量級的子進程,需要很少的內存才能執行。 通用多線程程序由兩個或多個并行運行的線程組成,以優化資源(多個CPU)的使用并提高程序的性能。

How thread works?

線程如何工作?

A thread is a lightweight process and its lifespan is defined as the time From which it starts to the point where it's terminated. In its lifespan, the thread goes from various phases. They are,

線程是輕量級進程,其壽命定義為從其開始到終止的時間。 在其生命周期中,線程來自不同的階段。 他們是,

new → runnable → running → terminated

新→可運行→運行→已終止

Sometimes the thread goes into the block state also.

有時,線程也會進入阻塞狀態。

Scala中的線程 (Threads in Scala)

To create a thread in Scala there are classes and methods defined. Threads in scala are created using two mechanisms,

要在Scala中創建線程,需要定義一些類和方法。 Scala中的線程是使用兩種機制創建的,

  1. Extending the Thread class

    擴展Thread類

  2. Extending the Runnable Interface

    擴展可運行接口

1)創建一個擴展Thread類的線程 (1) Creating a thread extending the Thread class)

The thread class is an inbuilt Scala class that is extended to create threads. It has run() method, which is overrated to run the side object.

線程類是內置的Scala類,可擴展為創建線程。 它具有run()方法,該方法被高估了以運行邊對象。

To create a thread we make an object of this class and then invoke the start() method which itself invokes the run() method.

要創建線程,我們創建此類的對象,然后調用start()方法,該方法本身也會調用run()方法。

Example:

例:

class MyThread extends Thread 
{ 
override def run(){ 
println("Hello, This is Thread " + Thread.currentThread().getName() ) 
} 
} 
object myObject { 
def main(args: Array[String]){ 
for (x <- 1 to 3){ 
var th = new MyThread() 
th.setName(x.toString()) 
th.start() 
} 
} 
} 

Output

輸出量

Hello, This is Thread 1
Hello, This is Thread 2
Hello, This is Thread 3

2)使用可運行接口創建線程 (2) Creating a thread using runnable interface)

Create thread using runnable interface to create a class that will implement the runnable interface and overwrites it's run() method to run a thread. Create a red Devil make an object of a class and then using this object we will call the start method that will initiate the thread and then automatically call the run()?method that runs thread.

使用runnable接口創建線程以創建將實現runnable接口并覆蓋其run()方法以運行線程的類。 創建一個紅色的Devil,使其成為類的對象,然后使用該對象,調用將啟動線程的start方法,然后自動調用運行線程的run()方法。

The below sample code shows how we create a thread using runnable interface:

下面的示例代碼顯示了我們如何使用可運行接口創建線程:

class MyThread extends Runnable{ 
override def run(){ 
println("Hello, This is Thread " + Thread.currentThread().getName() )
} 
} 
object myClass{ 
def main(args: Array[String]){ 
for (x <- 1 to 3){ 
var newThread = new Thread(new MyThread()) 
newThread.setName(x.toString()) 
newThread.start() 
} 
} 
} 

Output

輸出量

Hello, This is Thread 1
Hello, This is Thread 2
Hello, This is Thread 3

翻譯自: https://www.includehelp.com/scala/multithreading-in-scala.aspx

scala 多線程

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

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

相關文章

split注意事項

為什么80%的碼農都做不了架構師&#xff1f;>>> 1.特殊字符 “|”&#xff0c;“*”&#xff0c;“^”&#xff0c;"."&#xff0c;“&#xff1a;”&#xff0c;使用此字符作為分割符&#xff0c;必須用\\加以轉義 2.同時存在多個特殊字符的時候&#x…

Harbor升級和數據庫遷移手冊

Harbor升級和數據庫遷移手冊當升級一個已經存在的Harbor實例到新版本時&#xff0c;需要遷移數據庫數據。參考Whats New in Harbor Database Schema查看數據庫發生了哪些變化&#xff0c;如果有的話&#xff0c;就需要進行數據庫遷移操作&#xff0c;因為遷移可能會改變數據庫模…

Floyd Warshall算法

Description: 描述&#xff1a; This is a very popular interview problem to find all pair shortest paths in any graph. This problem has been featured in interview rounds of Samsung. 這是一個非常流行的面試問題&#xff0c;用于在任何圖中找到所有對最短路徑。 該…

Java多線程系列--“基礎篇”09之 interrupt()和線程終止方式

2019獨角獸企業重金招聘Python工程師標準>>> Java多線程系列--“基礎篇”09之 interrupt()和線程終止方式 概要 本章&#xff0c;會對線程的interrupt()中斷和終止方式進行介紹。涉及到的內容包括&#xff1a;1. interrupt()說明2. 終止線程的方式 2.1 終止處于“阻…

mac活動監視器_什么是活動監視器?

mac活動監視器活動監控 (Activity Monitor) Apple OS X provides the services of which one of them is Activity Monitor. Activity Monitor is used to monitor the activities of computer like active processes, processor load, applications that are running, and the…

concurrent包下的Exchanger練習

Exchanger可以在兩個線程之間交換數據&#xff0c;只能是2個線程&#xff0c;他不支持更多的線程之間互換數據。 當線程A調用Exchange對象的exchange()方法后&#xff0c;他會陷入阻塞狀態&#xff0c;直到線程B也調用了exchange()方法&#xff0c;然后以線程安全的方式交換數據…

Python默認參數

Python | 默認參數 (Python | default parameters) A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesnt provide a value for the parameter with the default value. …

最長公共前綴_最長的公共前綴

最長公共前綴Problem statement: 問題陳述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. 編寫函數以在字符串數組中找到最長的公共前綴字符串 。 If there is no common prefix, return an empty string "&quo…

物聯網聽起來像是一個和互聯網不同的網,萬物互聯又把網給弄丟了,正向我們撲面而來的是萬物互聯網。...

物聯網聽起來像是一個和互聯網不同的網&#xff0c;"萬物互聯"又把"網"給弄丟了&#xff0c;正向我們撲面而來的是"萬物互聯網"。轉載于:https://www.cnblogs.com/beingonline/p/7484135.html

sdram trp_TRP的完整形式是什么?

sdram trpTRP&#xff1a;電視收視點 (TRP: Television Rating Point) TRP is an abbreviation of "Television Rating Point". TRP是“電視評分點”的縮寫 。 It is a system or standard of measurement which signifies the demand and popularity of a televisi…

Controller計算值傳到jsp頁面,用session傳值

HttpSession session request.getSession(); session.setAttribute("key",value); jap 用 ${key}來接收該值 轉載于:https://www.cnblogs.com/douder/p/7484491.html

CBT的完整形式是什么?

CBT&#xff1a;基于計算機的培訓 (CBT: Computer Based Training) CBT is an abbreviation of "Computer-based training". CBT是“基于計算機的培訓”的縮寫 。 It is a training program which entails the use of a personal system or networked computer. The…

論道社會化商業

主持人 用友優普副總裁傅毅&#xff1a; 謝謝各位嘉賓&#xff0c;我們會留一些時間讓在座的嘉賓提問。請各位嘉賓用一個非常簡單的一句話&#xff0c;或者幾個關鍵詞&#xff0c;總結一下你認為的社會化商業是什么&#xff1f; 用友優普執行總裁 徐洋&#xff1a; 社會化商業為…

CChelper彩虹SDK可視遠程客服解決方案

本文講的是 : CChelper彩虹SDK可視遠程客服解決方案 , 在智能生態產業鏈中&#xff0c;智能硬件終端是把握消費者的直接環節&#xff0c;隨著物聯網時代邁向成熟&#xff0c;智能家居領域的硬件逐漸成為智能硬件終端的主角。目前的市場環境下&#xff0c;智能家居領域的自身硬…

matlab 簡介_MATLAB簡介

matlab 簡介MATLAB簡介 (MATLAB Introduction) MATLAB was designed by Cleve Moler for his student in 1970s but after some time jack little, an engineer realized its potential and rewrote it at the MathWorks, and it was rewritten in C language by the date of 1…

Scala中的嵌套循環

Scala中的嵌套循環 (Nested loop in Scala) In programming, a nested loop is used in initializing or iterate multi-dimensional array or to print patterns. Scala provides an efficient method to use nested loops in the programming language. The most used nested…

python基礎-字典

字典 # 字典是python基本數據結構之一&#xff0c;相對于列表和元組&#xff0c;他是無序的&#xff0c;每次輸出都打亂了順序&#xff0c;沒有下標hello{110:{"name":"alex","age":28,"home":"shandong"},111:{"name&…

sql算術運算符_SQL中的算術運算符

sql算術運算符SQL | 算術運算符 (SQL | Arithmetic Operators) Different number-crunching administrators are utilized in SQL to be specific Addition (), Subtraction (-), Multiplication (*), Division (/), Modulus (%). SQL中使用了不同的數字運算管理員來表示特定的…

HDU 6188 Duizi and Shunzi

棧。 將數字排序后&#xff0c;一個一個壓入棧。如果棧頂兩個元素形成了對子&#xff0c;那么$ans1$&#xff0c;彈出棧頂兩個元素&#xff1b;如果棧頂三個元素形成了順子&#xff0c;那么$ans1$&#xff0c;彈出棧頂三個元素。 #include<bits/stdc.h> using namespace …

php 單例模式有什么缺點_PHP的完整形式是什么?

php 單例模式有什么缺點PHP&#xff1a;超文本預處理器 (PHP: Hypertext Preprocessor ) PHP is an abbreviation of Hypertext Preprocessor, earlier called Personal Home Page. PHP is extensively used HTML-embedded, open-source server-side scripting language create…