groovy import java.util.concurrent.Executors def threadPool = Executors.newFixedThreadPool(5) def tasks = [ { println "Task 1" }, { println "Task 2" }, { println "Task 3" }, { println "Task 4" }, { println "Task 5" }, ] tasks.each { task -> threadPool.submit(task as Runnable) } threadPool.shutdown() groovy import java.util.concurrent.atomic.AtomicInteger def counter = new AtomicInteger(0) def incrementCounter() { counter.incrementAndGet() } def threads = [] 1.upto(10) { threads << Thread.start { incrementCounter() } } threads.each { it.join() } println counter.get() groovy def sharedResource = [] def synchronizedMethod() { synchronized(sharedResource) { } } def threads = [] 1.upto(10) { threads << Thread.start { synchronizedMethod() } } threads.each { it.join() }


上一篇:
下一篇:
切换中文