Created
May 17, 2020 14:12
-
-
Save wangzaixiang/06cded255a36600c8b8d31c677d49e1e to your computer and use it in GitHub Desktop.
Loom Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.management.{ManagementFactory, MemoryUsage} | |
import java.util.concurrent.CountDownLatch | |
import java.util.concurrent.atomic.AtomicInteger | |
object Main { | |
def main(args: Array[String]): Unit = { | |
Thread.startVirtualThread { () => | |
println(s"Hello ${Thread.currentThread()}") | |
} | |
Thread.sleep(1000) | |
test1(virtual = true) | |
test1(virtual = false) | |
} | |
def showHeap(): Unit ={ | |
val bean = ManagementFactory.getMemoryMXBean() | |
val heap = bean.getHeapMemoryUsage | |
val nonHeap: MemoryUsage = bean.getNonHeapMemoryUsage | |
println(s"heap used:${heap.getUsed} nonheap:${nonHeap.getUsed}") | |
} | |
def test1(virtual: Boolean): Unit ={ | |
val LOOP = 1000 | |
val latches: Array[CountDownLatch] = new Array[CountDownLatch](LOOP) | |
for(i <- 0 until LOOP) | |
latches(i) = new CountDownLatch(1) | |
val complete = new CountDownLatch(LOOP * 1000) | |
val count = new AtomicInteger(0) | |
case class Task( id: Int ) extends Runnable { | |
override def run(): Unit = while(true) { | |
latches(id).await() | |
latches(id) = new CountDownLatch(1) | |
count.incrementAndGet() | |
latches( (id+1) % LOOP ).countDown() | |
complete.countDown() | |
} | |
} | |
val tasks = new Array[Task](LOOP) | |
val begin = System.currentTimeMillis() | |
var i = 0 | |
while(i < LOOP) { | |
tasks(i) = Task(i) | |
if(virtual) | |
Thread.startVirtualThread(tasks(i)) | |
else { | |
val thread = new Thread(tasks(i)) | |
thread.setDaemon(true) | |
thread.start() | |
} | |
i += 1 | |
} | |
latches(0).countDown() | |
complete.await() | |
val end = System.currentTimeMillis() | |
println(s"virual:${virtual} total time:${end-begin}ms count=${count.get}" ) | |
System.gc() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment