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
ThreadLocal
is 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 passThreadLocal
Variables share data between the main thread and the child thread.
After setting the value in the main thread, the child thread can passget
The method gets the same value.
Share data through concurrent collection
You can use concurrent collections in Java, such asConcurrentHashMap
To 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 useConcurrentHashMap
Share 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 useLinkedBlockingQueue
As 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 asConcurrentHashMap
et 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.