Scala创建线程和用法示例

本文概述

有两种创建线程的方法:

  1. 通过扩展Thread类
  2. 通过实现Runnable接口

通过扩展线程类的Scala线程示例

下面的示例扩展Thread类并覆盖run方法。 start()方法用于启动线程。

class ThreadExample extends Thread{
override def run(){
println("Thread is running...");
}
}
object MainObject{
def main(args:Array[String]){
var t = new ThreadExample()
t.start()
}
}

输出

Thread is running...

通过扩展可运行接口的Scala线程示例

下面的示例实现Runnable接口并覆盖run方法。 start()方法用于启动线程。

class ThreadExample extends Runnable{
override def run(){
println("Thread is running...")
}
}
object MainObject{
def main(args:Array[String]){
var e = new ThreadExample()
var t = new Thread(e)
t.start()
}
}

输出

Thread is running...
微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_47728.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?