LinkedTransferQueue tryTransfer() method in Java with Examples

In Java, LinkedTransferQueue is a concurrent queue implementation which possesses the combination of characteristics from both the traditional blocking queues and the direct handoff queues. It implements the TransferQueue interface that is extending the BlockingQueue class and extends the functionalities of the producer-consumer scenario through the offering of additional methods.

The tryTransfer() method in the LinkedTransferQueue class is used to offer an immediately transfer of elements in the producer thread to the consumer thread that is waiting. If a waiting consumer doesn't exist, the method simply returns false without enqueuing the element and thus this method could be useful in the scenarios where the element has to move through immediately, but when no consumer is waiting, the producer can take a decision of how to handle the element.

Method Signature

Explanation

boolean: It returns a boolean value indicating whether the transfer was successful (true) or not (false).

tryTransfer: It is the name of the method.

(E e): It takes an argument of type E, which represents the type of elements held in the LinkedTransferQueue. The parameter e is the element to be transferred.

E: It is a placeholder for the type of elements in the queue, which is specified when LinkedTransferQueue is instantiated. For example, if you create a LinkedTransferQueue of integers, it would be LinkedTransferQueue<Integer>.

Example 1: Using with Multiple Producers

In the "Using with Multiple Producers" example, we'll explore how to use the tryTransfer() method of LinkedTransferQueue with multiple producer threads trying to transfer elements to a consumer and this example demonstrates coordination between multiple producers and a single consumer using LinkedTransferQueue.

Filename: MultipleProducersExample.java

Output

Producer 1 is trying to transfer element 1...
Producer 2 is trying to transfer element 2...
Consumer is trying to take element from queue...
Producer 1: Transfer successful.
Consumer received element: 1

Example 2: NullPointerException

In this example, we create a new LinkedTransferQueue object named transferQueue. It is parameterized with String, meaning it will hold elements of type String. A new thread is created using a lambda expression. Inside the thread, it attempts to transfer a null element using tryTransfer() method of transferQueue. If a NullPointerException occurs during the transfer, it's caught and printed.

Filename: Example.java

Output

Transferring an element...
java.lang.NullPointerException

Example 3: Transferring Objects

In this example, we demonstrate how to transfer objects between threads using the tryTransfer() method of LinkedTransferQueue. We'll create a simple producer-consumer scenario where a producer thread transfers a custom object to a consumer thread using a LinkedTransferQueue.

Filename: Example1.java

Output

Producer is trying to transfer data...
Consumer is trying to take data from queue...
Consumer received data: Data{name='Example'}
Data Data{name='Example'} transferred successfully.

Example 4: Handling Timeout

In this example, we demonstrate how to handle timeout scenarios when transferring elements using the tryTransfer() method of LinkedTransferQueue. In some cases, we may want to attempt a transfer but not wait indefinitely for a consumer to receive the element. We can specify a timeout duration, and if no consumer is available within that time, the transfer will not occur.

Filename: HandlingTimeoutExample.java

Output

Producer is trying to transfer element 42...
Timeout occurred, element 42 not transferred.

Example 5: Using with Multiple Producers and a Timeout

In this example, we demonstrate how to use tryTransfer() method with multiple producers and a timeout in a LinkedTransferQueue. In some scenarios, we may have multiple producers trying to transfer elements to a single consumer, and we want to handle situations where a transfer cannot be completed within a specified timeout duration.

Filename: MultipleProducersWithTimeoutExample.java

Output

Producer 1 is trying to transfer element 1...
Producer 2 is trying to transfer element 2...
Producer 1: Timeout occurred, element 1 not transferred.
Producer 2: Timeout occurred, element 2 not transferred.

Example 6: Non-Blocking Transfer

In this example, we demonstrate the use of the tryTransfer() method with a LinkedTransferQueue to perform a non-blocking transfer of elements between threads. In contrast to the transfer() method, which blocks until a consumer is available to receive the element, tryTransfer() returns immediately, indicating whether the transfer was successful or not.

Filename: NonBlockingTransferExample.java

Output

Producer is trying to transfer element 42...
Element 42 not transferred.