File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes Data exchange between threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Data exchange between threads" Watch "Data exchange between threads" New topic

Data exchange between threads

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Good morning Java guru's
Right now I am studying Java Threads and i have few question about data exchange between different threads:

1. How many basic ways / methods / mechanismes are there to exchange data between threads?
What i've read, seen, found online so far is:
A - Thread1 ---> "Queue" device <---- Thread2
B - Thread2 ---> "Pipeline" device <--- Thread2
C - Thread2 ---> "Stack" device" <--- Thread2

2. Is it possible to exchange data DIRECTLY between Threads_Objects like this?
||Methode_1()|| --[access Variable_2]-->..||.Variable_2.||
So that a method, function from Thread_1 object is able to modify data in the Thread_2 object directly?

3. Do you know a website with CLEAR and SIMPLE examples that teaches the very thread basics in SIMPLE steps?

Thank you very much for helping me unthreading this data exchange mistery :-)
Regards, Ronald
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

1: I would probably add a one more - Direct Reference. The mechanisms you present are tools that you use to minimize the synchronization needs of using the same reference in multiple threads. So when possible use the higher level mechanisms like concurrent Queues, Stacks, and Pipes.

2: Yes. You need to use synchronized blocks to refer to data safely in this manner. And you have to be real careful. But you can use the same Object reference or primitive in multiple threads.

3: The Java Tutorial of course: Though there is no 'easy' formula for making thread safe apps (except by not using Threads...). You just have to be aware of the gotchas, develop good tests and recognize the problems when they occur (hopefully in your tests, not in the wild).

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 443

.. a good place to start is to understand the volatile keyword (I'm not suggesting using it just understand what it does) then move to the "java Memory model" JSR 133 and if your brave the new fences API.

Simple Synchronization is by far the simplest solution to most of the issues but there a fair few people out there scared of deadlock and performance issues that refuse simple synchronization, while attempting some very complex thread use on these grounds but have no concept of the visibility issues they are leaving themselves open to :-(


Java Concurrency in practice comes highly recommended too.

"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
@Steve & Chris:
Thanks for answering my questions!

I have come up with this "solution" (scheduler and thread) example that works:
- Scheduler creates and starts 2 threads, exits when both threads are ready.
- The 2 Threads are able to run "parallel" / next to each-other / simultaneously"
- Scheduler can "inject" and "readback"values into a thread (internal) varible via get.. and set.. methodes

This is the output of the program:
Scheduler: Scheduler object created
Scheduler: Thread 1 created
Scheduler: Thread 2 created
Scheduler: Thread 1 started
ThreadID:1 - START
ThreadID:1 - 0 - 0
Scheduler: Thread 2 started
ThreadID:2 - START
ThreadID:2 - 0 - 0
Scheduler: Entering data into Thread 1
Thread: 1 recieved EXTERNAL value: [1]
Scheduler: Entering data into Thread 2
Thread: 2 recieved EXTERNAL value: [10]
Scheduler: Thread 1 - internal value : [1]
Scheduler: Thread 2 - internal value : [10]
Thread: 1 - INCREMENT -> value: [2]
Thread: 1 - INCREMENT -> value: [3]
Thread: 2 - DECREMENT -> value: [9]
Thread: 2 - DECREMENT -> value: [8]
Thread: 2 - DECREMENT -> value: [7]
Scheduler: Thread 1 - internal value : [3]
Scheduler: Thread 2 - internal value : [7]
ThreadID:1 - 1 - 3
ThreadID:2 - 1 - 7
ThreadID:1 - 2 - 3
ThreadID:2 - 2 - 7
ThreadID:1 - 3 - 3
ThreadID:2 - 3 - 7
ThreadID:1 - 4 - 3
ThreadID:2 - 4 - 7
Scheduler: End of Scheduler Object

- Is this "good inter-thread communication programming practice" / the right way to achieve this?
- Should i use synchronized blocks in my code? (I have not figured this feature out yet)?
- Are there any (hidden) hazards when e.g. implementing 100 threads this way?

Regards, Ronald

I agree. Here's the link:
subject: Data exchange between threads
jQuery in Action, 3rd edition