SHARE
TWEET

Multithread

Leejiaxin Oct 10th, 2019 (edited) 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. /*
  3. import scala.concurrent.ExecutionContext
  4. import scala.io.{BufferedSource, Source}
  5.  
  6. object ExecutionContextGlobal extends App {
  7.   //val data: BufferedSource = Source.fromFile("Write.txt")
  8.   //val iter: Iterator[String] = data.getLines()
  9.   //val dataString: String = iter.next()
  10.   //val strings: Iterable[String] = dataString.split(",")
  11.   //println(strings.size)
  12.   def execute(body: =>Unit) =
  13.     ExecutionContext.global.execute()
  14.   val iter: Iterator[String] = Source.fromFile("Write.txt").getLines()
  15.   val dataString: String = iter.next()
  16.   val strings: Iterable[String] = dataString.split(",")
  17.   val ints: Iterable[Int] = for(num <- strings) yield {
  18.     num.toInt
  19.   }
  20.   val separated: (Iterable[Int], Iterable[Int]) = ints.splitAt(ints.size / 2)
  21.   var sum1 = 0
  22.   var sum2 = 0
  23.   execute{
  24.     sum1 = separated._1.sum
  25.     println(sum1 + sum2)
  26.   }
  27.   execute{
  28.     sum2 = separated._2.sum
  29.     println(sum1 + sum2)
  30.   }
  31.   Thread.sleep(30000)
  32. }
  33. */
  34.  
  35. ///////
  36.  
  37. /*
  38. object ThreadSleep extends App {
  39.   def thread(body: =>Unit): Thread = {
  40.     val t = new Thread {
  41.       override def run() = body
  42.     }
  43.     t.start()
  44.     t
  45.   }
  46.  
  47.   val t = thread {
  48.     Thread.sleep(1000)
  49.     println("new thread running")
  50.     Thread.sleep(1000)
  51.     println("still running")
  52.     Thread.sleep(1000)
  53.     println("comopleted")
  54.   }
  55.   t.join()
  56.   println("new thread joined")
  57. }
  58. */
  59.  
  60. import java.util.concurrent.{ForkJoinPool, _}
  61.  
  62. import scala.io.Source
  63.  
  64. object MyProgram2 extends App {
  65.   val executor = new ForkJoinPool
  66.   executor.execute(new Runnable{
  67.     def run() = println("This task run asynchronously, MyProgram2")
  68.   })
  69.   Thread.sleep(500)
  70. }
  71.  
  72. object ExecutorServiceCreate extends App {
  73.   val executor: ExecutorService = Executors.newFixedThreadPool(4)
  74.   executor.execute(new Runnable {
  75.     def run() =  println("This task run asynchronously, ESCreate")
  76.   } )
  77.   executor.shutdown()
  78.   executor.awaitTermination(60, TimeUnit.SECONDS)
  79. }
  80.  
  81. /*
  82. object ExecutionContextGlobal extends App {
  83.   def execute(body: =>Unit) = ExecutionContext.global.execute( new Runnable {def run() = body})
  84.   for (i <- 0 until 32) execute {
  85.     Thread.sleep(2000)
  86.     println(s"Task $i completed")
  87.   }
  88.   Thread.sleep(10000)
  89. }
  90. */
  91.  
  92. object ExecutionContextGlobal extends App{
  93.   val iter: Iterator[String] = Source.fromFile("Write.txt").getLines()
  94.   val dataString:String = iter.next()
  95.   val strings: Iterable[String] = dataString.split(",")
  96.   val ints: Iterable[Int] = for (num<-strings) yield{ num.toInt}
  97.   val sep:(Iterable[Int], Iterable[Int]) = ints.splitAt(ints.size/2)
  98.   sep._1
  99.   sep._2
  100.   println(ints.sum)
  101. }*/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top