如何解决无法在Jenkins Groovy管道中产生线程
我想在Groovy管道中运行多个线程。这是我的管道
node{
List<Thread> cleanupThreads = new ArrayList<>(100);
"""1 2 3 4 5 6""".split().each{
def th = new Thread({
Random rand = new Random()
def wait = (long)(rand.nextDouble() * 1000)
this.sleep wait
println "Running Thread ${this.getName()}"
})
cleanupThreads.add(th)
}
cleanupThreads.each {it.start() }
cleanupThreads.each {it.join() }
}
运行它时,在控制台中看不到任何“运行线程$ {this.getName()}”输出行
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test_jobs/multithreaded_groovy_pipeline
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
有什么想法吗? Jenkins管道是否支持多线程?
解决方法
我不确定为什么要将groovy多线程功能编码到不建议使用的管道脚本中。如果要并行运行其他作业,则可以使用Parallel阶段功能。请在下面的示例中找到
pipeline {
agent any
stages {
stage ("Test Stage"){
steps {
parallel(
job1: {
echo "Inside 1st Job"
},job2: {
echo "Inside 2nd Job"
},job3: {
echo "Inside 3rd Job"
}
)
}
}
}
}
输出
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。