Code 1:
package jerry;
Runnable run = { println 'run in thread: ' + Thread.currentThread().getId() }
run.run();
println "main thread:" + Thread.currentThread().getId();
// list.each { println it } // or list.each(this.&println)
Copy the code
Test results:
Call Runnable’s run method directly, and the code inside the method is also executed in the main thread:
Code 2:
package jerry;
Runnable run = { println 'run in thread: ' + Thread.currentThread().getId() }
new Thread(run).start();
println "main thread:" + Thread.currentThread().getId();
// list.each { println it } // or list.each(this.&println)
Copy the code
The Runnable body is executed in the newly started thread:
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: