Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Data exchange between threads

 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
===========.............................................==========
||..Thread_1...||............................................||..Thread_2..||
||....................||............................................||---------------||
||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
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://java.sun.com/docs/books/tutorial/essential/concurrency/. 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
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.. 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 :-(

PS

Java Concurrency in practice comes highly recommended too.
 
Ronald Vermeij
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
ThreadID:1 - TERMINATED
ThreadID:2 - TERMINATED
Scheduler: End of Scheduler Object


Questions:
- 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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic