SoFunction
Updated on 2025-04-06

Implement shared data transfer between java parent and child threads

In Java programming, sharing data passing between parent and child threads is a common problem. This article will introduce several methods to implement data sharing between parent and child threads, and remind you to pay attention to concurrency security issues.

Share data through ThreadLocal variables

ThreadLocalis a thread local variable that can provide independent copy of variables for each thread.

Here is a sample code:

public class ThreadLocalExample {
    public static void main(String[] args) {
        // Define ThreadLocal variable        ThreadLocal<String> threadLocal = new ThreadLocal<>();

        // Set the value in the main thread        ("The value of the main thread");

        // Create child thread        Thread childThread = new Thread(() -> {
            // Get the value in the child thread            String value = ();
            ("The value obtained by the child thread:" + value);
        });
        ();
    }
}

In the above code, we passThreadLocalVariables share data between the main thread and the child thread.

After setting the value in the main thread, the child thread can passgetThe method gets the same value.

Share data through concurrent collection

You can use concurrent collections in Java, such asConcurrentHashMapTo realize data sharing between parent and child threads.

Here is a sample code:

import ;

public class ConcurrentMapExample {
    public static void main(String[] args) {
        // Define ConcurrentHashMap        ConcurrentHashMap<String, String> concurrentMap = new ConcurrentHashMap<>();

        // Set the value in the main thread        ("key", "The value of the main thread");

        // Create child thread        Thread childThread = new Thread(() -> {
            // Get the value in the child thread            String value = ("key");
            ("The value obtained by the child thread:" + value);
        });
        ();
    }
}

In this example, we useConcurrentHashMapShare data between the main thread and the child thread.

After putting a key-value pair in the main thread, the child thread can obtain the corresponding value through the same key.

Share data through memory queues or message queues

You can define a blocking queue, such asBlockingQueue, put data into the queue in the main thread, and the child thread obtains data from the queue.

Here is a sample code:

import ;
import ;

public class QueueExample {
    public static void main(String[] args) throws InterruptedException {
        // Define a blocking queue        BlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>();

        // Put data in the main thread        ("The value of the main thread");

        // Create child thread        Thread childThread = new Thread(() -> {
            try {
                // Get data in child thread                String value = ();
                ("The value obtained by the child thread:" + value);
            } catch (InterruptedException e) {
                ().interrupt();
            }
        });
        ();
    }
}

In this example, we useLinkedBlockingQueueAs a memory queue, data is placed into the queue in the main thread, and the child thread obtains data from the queue.

Pay attention to concurrency security issues

When sharing data through multi-threaded operations, you need to pay attention to concurrency security issues.

To ensure consistency and correctness of the data, you can choose to use concurrent collections, such asConcurrentHashMapet al. These collections are designed with multi-threaded concurrent access in consideration, providing better concurrency security.

In short, there are many ways to share data between parent and child threads, and you can choose the appropriate method according to specific needs. At the same time, we should pay attention to concurrency security issues to ensure the correctness and stability of the program.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.