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中的線程是使用兩種機制創建的,
Extending the Thread class
擴展Thread類
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 多線程