我们如何在Scala中同时运行多个功能?

发布于 2025-01-31 11:54:53 字数 582 浏览 3 评论 0原文

这是我试图编写的Scala代码,用于与两个不同的参数同时多次处理功能。但是,我注意到这些功能是由一个一个,而不是同时执行的。

class Method1 extends Thread {
  override def run(): Unit ={
    println("Hello, Current running is Thread No. " + Thread.currentThread().getName )
    Function("Parameter 1")
    Function("Parameter 2")
  }
}
object Obj extends App {
    for (x <- 1 to 4){
      val th1 = new Method1()
      th1.setName(x.toString)
      th1.start()
    }
}
val a = Array("Justanormalarray")
Obj.main(a)

如何实现这一目标?抱歉,我看到的所有教程都只能解释一个非常基本的级别,即可以通过扩展线程类或使用可运行的界面来实现多线程,但似乎并没有清除此内容(即如何同时实际执行多个内容)

This is a scala code I'm trying to write for processing a function multiple times concurrently with two different parameters. However, I notice that the functions are being executed one by one and not both at same time.

class Method1 extends Thread {
  override def run(): Unit ={
    println("Hello, Current running is Thread No. " + Thread.currentThread().getName )
    Function("Parameter 1")
    Function("Parameter 2")
  }
}
object Obj extends App {
    for (x <- 1 to 4){
      val th1 = new Method1()
      th1.setName(x.toString)
      th1.start()
    }
}
val a = Array("Justanormalarray")
Obj.main(a)

How to achieve this? Sorry for being dumb but all tutorials I seen only explains a very basic level that multithreading can be achieved by extending thread class or by using runnable interface, but doesn't seem to clear this up (i.e. how to actually execute multiple stuff at same time)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文