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 多线程