Groovy 线程
来源:互联网 发布:淘宝cdk 编辑:程序博客网 时间:2024/05/16 14:27
首先申明下,本文为笔者学习《Groovy 程序设计》的笔记,并加入笔者自己的理解和归纳总结。
1、Groovy中,调用Thread的静态方法start来创建线程。
- def printThreadInfo() {
- Thread currentThread = Thread.currentThread()
- println "Current Thread is ${currentThread}"
- }
- printThreadInfo()
- Thread.start {
- printThreadInfo();
- }
Current Thread is Thread[main,5,main]
Current Thread is Thread[Thread-1,5,main]
2、sleep方法可以用来暂停当前操作。
- startTime = System.currentTimeMillis()
- new Object().sleep(2000)
- endTime = System.currentTimeMillis()
- println "Thread last ${endTime - startTime} MilliSeconds"
Thread last 2001 MilliSeconds
3、如果sleep方法返回true,inturrpt方法可以打断sleep。
- def interruptSleep(name, flag) {
- thread = Thread.start(name) {
- startTime = System.currentTimeMillis()
- new Object().sleep(2000) {
- flag
- }
- endTime = System.currentTimeMillis()
- Thread currentThread = Thread.currentThread()
- println "${currentThread} last ${endTime - startTime} MilliSeconds"
- }
- thread.interrupt()
- }
- interruptSleep("thread1", true)
- interruptSleep("thread2", false)
Thread[thread1,5,main] last 2 MilliSeconds
Thread[thread2,5,main] last 2001 MilliSeconds
4、Daemon线程在主线程退出后不再运行。
- Thread.start {
- startTime = System.currentTimeMillis()
- new Object().sleep(2000)
- endTime = System.currentTimeMillis()
- println "Thread last ${endTime - startTime} MilliSeconds"
- }
- Thread.startDaemon {
- startTime = System.currentTimeMillis()
- new Object().sleep(3000)
- endTime = System.currentTimeMillis()
- println "Daemon Thread last ${endTime - startTime} MilliSeconds"
- }
Thread last 2002 MilliSeconds
阅读全文
0 0
- Groovy 线程
- Groovy 线程
- Unmi 学习 Groovy 之多线程
- Unmi 学习 Groovy 之多线程
- 实战 Groovy(6)- 事件分派线程
- groovy
- groovy
- Groovy
- Groovy
- Groovy
- groovy
- groovy
- Groovy
- Groovy
- Groovy
- Groovy
- groovy
- Groovy
- 3
- 4
- 寿险常见核算科目及场景分录
- 【R和Python对比】数据导入和初步整理
- 5
- Groovy 线程
- 对OpenGLES2.0渲染管线的理解
- c#做端口转发程序支持正向连接和反向链接
- Unity3D中通过UNET对多个物体进行同步
- 剑指offer:(33)时间效率 :把数组排成最小的数
- 限流算法之漏桶算法、令牌桶算法
- 在win10利用Python3.6编写批量下载图片的小程序
- Spring注解的作用
- Netty系列之Netty线程模型