📜  Scala线程方法

📅  最后修改于: 2021-01-09 12:20:20             🧑  作者: Mango

Scala线程方法

线程类提供了各种方法来处理线程的状态。您可以使用这些方法来控制线程流。

下表包含Thread类的常用方法。

Method Description
public final String getName() It returns thread’s name.
public final int getPriority() It returns thread’s priority.
public Thread.State getState() It returns the state of this thread. This method is designed for use in monitoring of the system state, not for synchronization control.
public final boolean isAlive() It tests if this thread is alive. A thread is alive if it has been started and has not yet died.
public final void join() throws InterruptedException It Waits for thread to die.
public void run() If this thread was constructed using a separate Runnable run object, then that Runnable object’s run method is called; otherwise, this method does nothing and returns.
public final void setName(String name) It is used to set thread name.
public final void setPriority(int newPriority) It is used to set priority of a thread.
public static void sleep(long millis) throws InterruptedException It is used to sleep executing thread for the specified number of milliseconds.
public static void yield() It causes the currently executing thread object to temporarily pause and allow other threads to execute.

Scala线程sleep()方法

sleep()方法用于使线程休眠指定的时间。作为参数需要花费毫秒的时间。

class ThreadExample extends Thread{
override def run(){
for(i<- 0 to 5){
println(i)
Thread.sleep(500)
}
}

}

object MainObject{
def main(args:Array[String]){
var t1 = new ThreadExample()
var t2 = new ThreadExample()
t1.start()
t2.start()
}
}

输出:

0
0
1
1
2
2
3
3
4
4
5
5

Scala线程join()方法示例

join()方法等待线程死亡。换句话说,join()方法用于保留当前正在运行的线程的执行,直到指定线程完成执行为止。

class ThreadExample extends Thread{
override def run(){
for(i<- 0 to 5){
println(i)
Thread.sleep(500)
}
}

}
object MainObject{
def main(args:Array[String]){
var t1 = new ThreadExample()
var t2 = new ThreadExample()
var t3 = new ThreadExample()
t1.start()
t1.join()
t2.start()
t3.start()
}
}

输出:

0
1
2
3
4
5
0
0
1
1
2
2
3
3
4
4
5
5

Scala setName()方法示例

在下面的示例中,我们正在设置并获取线程名称。

class ThreadExample() extends Thread{
override def run(){
for(i<- 0 to 5){
println(this.getName()+" - "+i)
Thread.sleep(500)
}
}
}

object MainObject{
def main(args:Array[String]){
var t1 = new ThreadExample()
var t2 = new ThreadExample()
var t3 = new ThreadExample()
t1.setName("First Thread")
t2.setName("Second Thread")
t1.start()
t2.start()
}
}

输出:

First Thread - 0
Second Thread - 0
Second Thread - 1
First Thread - 1
Second Thread - 2
First Thread - 2
Second Thread - 3
First Thread - 3
Second Thread - 4
First Thread - 4
Second Thread - 5
First Thread - 5

Scala线程优先级示例

您可以使用线程的预定义方法设置线程优先级。下面的示例设置线程的优先级。

class ThreadExample() extends Thread{
override def run(){
for(i<- 0 to 5){
println(this.getName())
println(this.getPriority())
Thread.sleep(500)
}
}
}
object MainObject{
def main(args:Array[String]){
var t1 = new ThreadExample()
var t2 = new ThreadExample()
t1.setName("First Thread")
t2.setName("Second Thread")
t1.setPriority(Thread.MIN_PRIORITY)
t2.setPriority(Thread.MAX_PRIORITY)
t1.start()
t2.start()
}
}

输出:

First Thread
Second Thread
10
1
Second Thread
10
First Thread
1
Second Thread
10
First Thread
1
Second Thread
10
First Thread
1
Second Thread
10
First Thread
1
Second Thread
10
First Thread
1

Scala线程多任务示例

以下示例通过使用多个线程来运行多个任务。此示例说明了如何在Scala中实现多任务。

class ThreadExample() extends Thread{
override def run(){
for(i<- 0 to 5){
println(i)            
Thread.sleep(500)
}
}
def task(){
for(i<- 0 to 5){
println(i)
Thread.sleep(200)
}
}
}

object MainObject{
def main(args:Array[String]){
var t1 = new ThreadExample()
t1.start()
t1.task()         
}
}

输出:

0
0
1
2
1
3
4
2
5
3
4
5