Skip to content

Instantly share code, notes, and snippets.

@ugdark
Created March 25, 2016 06:54
Show Gist options
  • Save ugdark/00f75926da803032b415 to your computer and use it in GitHub Desktop.
Save ugdark/00f75926da803032b415 to your computer and use it in GitHub Desktop.
package com.example.collection
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Future}
/**
* Queueサンプル
*/
object QueueSample extends App {
{
val queue = new java.util.concurrent.ConcurrentLinkedQueue[String]()
Future {
var i = 0
while (true) {
i += 1
queue.add(i.toString)
if (i % 5 == 0) {
println(s"queue put. queue size:${queue.size}")
println(s"queue put. queue ${queue}")
Thread.sleep(5000)
}
}
}
Future {
var i = 0
while (true) {
i += 100
queue.add(i.toString)
if (i % 3 == 0) {
println(s"queue2 put. queue size:${queue.size}")
println(s"queue2 put. queue $queue")
Thread.sleep(2000)
}
}
}
val master = Future {
while (true) {
Thread.sleep(1000)
if (!queue.isEmpty) {
val s = queue.poll()
println(s"queue poll:$s queue size:${queue.size}")
}
}
}
Await.result(master, Duration.Inf)
// queue.add("a")
// queue.add("i")
// queue.add("u")
// println(queue)
// println(queue.poll())
// println(queue.poll())
// println(queue)
//
// while (true) {
// println((Runtime.getRuntime.totalMemory() - Runtime.getRuntime.freeMemory()) / 1000)
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment