How to pass a runnable procedure (a function) as a parameter and run it in Scala

1 Answer

0 votes
object runProcedureExample {
  def runProcedure(task: () => Unit): Unit = {
    val runnable = new Runnable {
      override def run(): Unit = task()
    }
    val thread = new Thread(runnable)
    thread.start()
  }

  def main(args: Array[String]): Unit = {
    // Define a function to pass as a parameter
    val say: () => Unit = () => println("abcd")

    // Pass the function to the procedure
    runProcedure(say)
  }
}


 
/*
run:

abcd

*/

 



answered May 22, 2025 by avibootz
...