Groovy multithreading

Groovy multithreading

 

Java supports multithreading. Defines the Runnable interface, and provides wait/notify methods in the root class Object, and supports the synchronized keyword. We often say that there are two ways to implement multithreading: inheriting the Thread class and implementing the Runnable interface. In essence, the worker thread executes the run() method defined in the Runnable interface. Thread itself implements the Runnable interface, but it is only for The use of thread scheduling provides many useful methods only. 


Groovy won't be slack when it comes to multithreading. Groovy extends java.lang.Thread through MetaClass, the so-called GDK - Groovy methods added to Java SE classes.

 

Two methods have been added to the original java.lang.Thread class, which are:

 

static Thread start(Closure closure);
static Thread startDaemon(Closure closure); //corresponding to the Daemon thread

 

The parameters accepted by these two methods are closures. You must know that all closures inherit from  groovy.lang.Closure , which implements java.lang.Thread . So multithreading in Groovy is easy to implement using closures . 

 

There are several ways to write the code as follows: 

 

t = new Thread() {/* Closure body */};
t.start();
 
Thread.start { /* Closure body */};
 
Thread.startDaemon { /* Closure body */};


This way, the code in the closure is executed in a new thread. Is it really? The most impressive way is to use a piece of code to test it. It can be observed from two aspects, and it is enough to satisfy one item:  1. Print the current thread name separately from the outside and inside of the closure to see if it is different. For different threads 2. See if the operation in the closure will block the main thread, if not, it means the closure executed in the new thread

 


 

println "Outter thread: " + Thread.currentThread().getName()
Thread.start {
        println "Inner thread: "+Thread.currentThread().getName()
        "How are you?".each {
                print it
        }
}
println "Fine, thank you."

 

The output after execution is:

Outter thread: main 
Fine, thank you. 
Inner thread: Thread-1 
How are you? 

 

The thread names are not the same, and the last line of code has a chance to execute before or during the execution of the closure, either of which means the closure is executed in a new thread. Try calling the closure in the usual way, it's a different story. 


In Java, Timer and TimerTask can be used to implement timed tasks - executed in a new thread. Groovy also has a corresponding extension to this java.util.Timer, adding the runafter(int delay,Closure closure) method. Therefore, using Timer in Groovy can also achieve multi-threading:

 

new Timer().runAfter(1000) { /* Closure body */}

 

Other thread-related wait/notify and synchronized controls are similar in use to Java.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326447173&siteId=291194637