scala 多线程_Scala中的多线程
scala 多線程
Scala多線程 (Scala Multithreading)
Multithreading is the concept of using multiple threads simultaneously that allows the program to perform multiple operations simultaneously.
多線程是同時使用多個線程的概念,它允許程序同時執(zhí)行多個操作。
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.
線程是輕量級的子進程,需要很少的內(nèi)存才能執(zhí)行。 通用多線程程序由兩個或多個并行運行的線程組成,以優(yōu)化資源(多個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.
有時,線程也會進入阻塞狀態(tài)。
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中創(chuàng)建線程,需要定義一些類和方法。 Scala中的線程是使用兩種機制創(chuàng)建的,
Extending the Thread class
擴展Thread類
Extending the Runnable Interface
擴展可運行接口
1)創(chuàng)建一個擴展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.
線程類是內(nèi)置的Scala類,可擴展為創(chuàng)建線程。 它具有run()方法,該方法被高估了以運行邊對象。
To create a thread we make an object of this class and then invoke the start() method which itself invokes the run() method.
要創(chuàng)建線程,我們創(chuàng)建此類的對象,然后調(diào)用start()方法,該方法本身也會調(diào)用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 32)使用可運行接口創(chuàng)建線程 (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接口創(chuàng)建線程以創(chuàng)建將實現(xiàn)runnable接口并覆蓋其run()方法以運行線程的類。 創(chuàng)建一個紅色的Devil,使其成為類的對象,然后使用該對象,調(diào)用將啟動線程的start方法,然后自動調(diào)用運行線程的run()方法。
The below sample code shows how we create a thread using runnable interface:
下面的示例代碼顯示了我們?nèi)绾问褂每蛇\行接口創(chuàng)建線程:
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 多線程
總結(jié)
以上是生活随笔為你收集整理的scala 多线程_Scala中的多线程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是html的混杂模式_HTML的完整
- 下一篇: [转载] python迭代器、生成器和装