Exchanger

It offers a simplified way of communicating between threads, that is, by passing a specific object between two threads. That's why there's the <V> after the class name.

```import java.util.*;
import java.util.concurrent.*;

public class ExchangerTest
{

private static final int FULL = 10;

private static final int COUNT = FULL * 20;

private static final Random random = new Random();

private static volatile int sum = 0;

//the constructor simply identifies the type of object to exchange
private static Exchanger<List<Integer>>
exchanger = new Exchanger<List<Integer>>();

private static List<Integer> initiallyEmptyBuffer;

private static List<Integer> initiallyFullBuffer;

private static CountDownLatch stopLatch = new CountDownLatch(2);

//Producer
private static class FillingLoop implements Runnable
{
public void run()
{
List<Integer> currentBuffer = initiallyEmptyBuffer;
try
{
for (int i = 0; i < COUNT; i++)
{
if (currentBuffer == null)
break; // stop on null
Integer item = random.nextInt(100);
if (currentBuffer.size() == FULL)
//when buffer is full exchange iteam with consumer
currentBuffer = exchanger.exchange(currentBuffer);
}
} catch (InterruptedException ex)
{
}
stopLatch.countDown();
}
}

//Consumer
private static class EmptyingLoop implements Runnable
{
public void run()
{
List<Integer> currentBuffer = initiallyFullBuffer;
try
{
for (int i = 0; i < COUNT; i++)
{
if (currentBuffer == null)
break; // stop on null
Integer item = currentBuffer.remove(0);
System.out.println("Got: " + item);
sum += item.intValue();
if (currentBuffer.isEmpty())
{
//when buffer is empty exchange iteam with producer
currentBuffer = exchanger.exchange(currentBuffer);
}
}
} catch (InterruptedException ex)
{
}
stopLatch.countDown();
}
}

public static void main(String args[])
{
initiallyEmptyBuffer = new ArrayList<Integer>();
initiallyFullBuffer = new ArrayList<Integer>(FULL);
for (int i = 0; i < FULL; i++)
{
}
{"ft_lang_label":"__label__en","ft_lang_prob":0.57062674,"math_prob":0.9019906,"size":2354,"snap":"2020-10-2020-16","text_gpt3_token_len":522,"char_repetition_ratio":0.16042553,"word_repetition_ratio":0.23178808,"special_character_ratio":0.2591334,"punctuation_ratio":0.21428572,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9616421,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-03-28T09:40:45Z\",\"WARC-Record-ID\":\"<urn:uuid:f4296ba9-cbcd-49ad-adb0-168ee62f106e>\",\"Content-Length\":\"49173\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b322dfba-eb4a-4d7c-b895-c79652235974>\",\"WARC-Concurrent-To\":\"<urn:uuid:25e0ce84-f1b6-416d-b2e6-01ad1267fb40>\",\"WARC-IP-Address\":\"107.20.139.176\",\"WARC-Target-URI\":\"http://programmingexamples.wikidot.com/exchanger\",\"WARC-Payload-Digest\":\"sha1:Q5KTDA552EK4PRKR3JSIU3NIKNVSQQV6\",\"WARC-Block-Digest\":\"sha1:RNUC4L7JJTFC64TJCBBLVWT6CNACMWEG\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-16/CC-MAIN-2020-16_segments_1585370490497.6_warc_CC-MAIN-20200328074047-20200328104047-00351.warc.gz\"}"}