Threading : - How can you pass data between two threads?

Questions by changaez

Showing Answers 1 - 14 of 14 Answers

boozeee

  • Dec 3rd, 2005
 

It is very simple.

Get the data that has to be passed from one thread.

Set the data in a global variable.

now get the data from the glo bal variable !!

It is one of the easiest questions i have ever heard !

  Was this answer useful?  Yes

MK

  • Dec 6th, 2005
 

You can use a piped input/output stream.

A piped input stream should be connected to a piped output stream; the piped input stream then provides whatever data bytes are written to the piped output stream. Typically, data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread. The piped input stream contains a buffer, decoupling read operations from write operations, within limits.

  Was this answer useful?  Yes

chaitanya

  • Dec 13th, 2005
 

using join() method we can get data from one thread to other thread.

  Was this answer useful?  Yes

Navjot Singh

  • Feb 7th, 2006
 

1. use global varibales

2. use named pipe streams

3. use socket

4. use file system

whatever works

  Was this answer useful?  Yes

Ravi Kumar

  • Aug 7th, 2006
 

In Java, it is not possible to pass data between two threads.

  Was this answer useful?  Yes

amit saini

  • Jan 8th, 2007
 

Java simplifies I/O by providing a rich library of classesthat support stream interfaces. Among these classes, the combination ofPipedInputStream and PipedOutputtStream closely resembles a UNIX system"pipe." The purpose of these "piped stream" classes is toallow the program to read data into an input stream and write it out through anoutput stream in a single method call.

Pipes are typically used in threaded programs. Consider a program thatspawns two threads. One of the threads writes out data that it reads from adata source. The other reads this data and processes it. One way to achievethis functionality is to open an output stream in the first thread and have itcontinuously write out the data to some shared storage. You would also have toopen an output stream (in the second thread), which would read in the data fromthe shared storage.

An alternative is to use the "piped stream" classes. In order toachieve the same objective, instantiate a PipedInputStream and aPipeOutputStream. Then connect the two streams so that the writes from theoutput stream go to the input stream. After that, every time the first threadwrites some data using the write() call, a corresponding read() on the inputstream will make it available to the second thread.

  Was this answer useful?  Yes

Adham

  • Jun 12th, 2014
 

Passing variables through global variable can result in unexpected problems as many threads may try to modify this variable at the same time.
Programing languages provide different ways to deal with this problem
as delegates and invocation, pipes, handlers and loppers.

  Was this answer useful?  Yes

Sandeep

  • Jul 10th, 2015
 

Chaitanya, Can you please explain me, How join() works?
I am in a situation where two clients (two different threads) are sending data to server and job of server is to fetch data from one client (thread) and feed to another client (thread), but I am unable to identify threads.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions