読者です 読者をやめる 読者になる 読者になる

by shigemk2

当面は技術的なことしか書かない

sbtでスレッド処理のようなことをやる

Scala

構成。

$ tree
.
└── thread.scala
0 directories, 1 file

thread.scala

object Main {
  def main(args: Array[String]) = {
    println("ThreadSample#main start")
    for(i<-0 to 3) {
      val t = new Thread(new ThreadSample("[Thread-" + i + "]"))
      t.start()
    }
    println("ThreadSample#main end")
  }
}

class ThreadSample(name: String) extends Runnable {
  def run() = {
    for(i<-0 to 3) {
      println(name + "count=" + i)
      Thread.sleep(1000)
    }
  }
}

sbt実行。

$ sbt
[warn] The global sbt directory is now versioned and is located at /Users/shige/.sbt/0.13.
[warn]   You are seeing this warning because there is global configuration in /Users/shige/.sbt but not in /Users/shige/.sbt/0.13.
[warn]   The global sbt directory may be changed via the sbt.global.base system property.
[info] Set current project to scala-thread (in build file:/Users/shige/junk/2014/03/scala-thread/)
> run
[info] Updating {file:/Users/shige/junk/2014/03/scala-thread/}scala-thread...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/shige/junk/2014/03/scala-thread/target/scala-2.10/classes...
[info] Running Main
ThreadSample#main start
[Thread-2]count=0
[Thread-0]count=0
[Thread-1]count=0
ThreadSample#main end
[Thread-3]count=0
[Thread-2]count=1
[Thread-1]count=1
[Thread-0]count=1
[Thread-3]count=1
[Thread-1]count=2
[Thread-3]count=2
[Thread-2]count=2
[Thread-0]count=2
[Thread-1]count=3
[Thread-2]count=3
[Thread-3]count=3
[Thread-0]count=3
[success] Total time: 17 s, completed 2014/03/14 20:56:48