Use of Java Concurrent exchange

brief introduction

Exchange is a concurrent class introduced by Java 5. As its name implies, exchange is used for exchange. Here is mainly the exchange of objects held between two threads. When Exchanger invokes the exchange method in a thread, it will wait for another thread to call the same exchange method.

After both threads call the exchange method, the parameters passed in are exchanged.

Class definition

public class Exchanger<V>

Where V represents the object type to be exchanged.

Class inheritance

java.lang.Object
↳ java.util.concurrent.Exchanger<V>

Exchange inherits directly from object.

Constructor

Exchanger() 

Exchange provides a parameterless constructor.

Two main methods

When this method is called, the current thread will wait until other threads call the same method. When other threads call exchange, the current thread will continue to execute.

During the waiting process, if other threads interrupt the current thread, an interruptedexception will be thrown.

Similar to the first method, the difference is that there is an additional timeout time. If no other thread calls the exchange method within the timeout time, a timeoutexception is thrown.

Specific examples

Let's first define a class with exchange:

@Data
public class CustBook {

    private String name;
}

Then define two Runnable and call the exchange method in the run method:

@Slf4j
public class ExchangerOne implements Runnable{

    Exchanger<CustBook> ex;

    ExchangerOne(Exchanger<CustBook> ex){
      this.ex=ex;
    }

    @Override
    public void run() {
    CustBook custBook= new CustBook();
        custBook.setName("book one");

        try {
            CustBook exhangeCustBook=ex.exchange(custBook);
            log.info(exhangeCustBook.getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
@Slf4j
public class ExchangerTwo implements Runnable{

    Exchanger<CustBook> ex;

    ExchangerTwo(Exchanger<CustBook> ex){
      this.ex=ex;
    }

    @Override
    public void run() {
    CustBook custBook= new CustBook();
        custBook.setName("book two");

        try {
            CustBook exhangeCustBook=ex.exchange(custBook);
            log.info(exhangeCustBook.getName());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Finally, in the main method, we call:

public class ExchangerUsage {

    public static void main(String[] args) {
        Exchanger<CustBook> exchanger = new Exchanger<>();
        // Starting two threads
        new Thread(new ExchangerOne(exchanger)).start();
        new Thread(new ExchangerTwo(exchanger)).start();
    }
}

Let's look at the results:

22:14:09.069 [Thread-1] INFO com.flydean.ExchangerTwo - book one
22:14:09.073 [Thread-0] INFO com.flydean.ExchangerOne - book two

You can see that the object has been exchanged.

epilogue

Exchange is very useful when two threads need to exchange objects. It can be used in real work and life.

Examples of this article https://github.com/ddean2009/learn-java-concurrency/tree/master/Exchanger

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>