pls help me out

1. class : MainClass

import java.io.*;

publicclass MainClass

{

publicstaticvoid main(String s[])throws IOException

{

DataInputStream in=new DataInputStream(System.in);

new Reader1(in).start();

new Reader2(in).start();

}

}

2. class : Reader1

import java.io.*;

publicclass Reader1extends Thread

{

DataInputStream din;

public Reader1(DataInputStream in)

{

super("Thread 1");

din=in;

}

publicvoid run()

{

String str;

try

{

while((str=din.readLine())!=null)

System.out.println("reader1 :"+str);

}

catch(IOException e)

{e.printStackTrace();System.exit(1);}

}

}

3. class : Reader2

import java.io.*;

publicclass Reader2extends Thread

{

DataInputStream din;

public Reader2(DataInputStream in)

{

super("Thread 2");

din=in;

}

publicvoid run()

{

String str;

try

{

while((str=din.readLine())!=null)

System.out.println("reader2 :"+str);

}

catch(IOException e)

{e.printStackTrace();System.exit(1);}

}

}

what change i need to make to the above code so that input is read by both the threads...

Answer to this question will help me in my project where i've a similar situation

[3258 byte] By [manishmulani@nitka] at [2007-11-27 7:33:57]
# 1
i mean every input from keyboard must b read by both the Threads
manishmulani@nitka at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 2
You'll need an intermediate buffer which reads from your InputStream and then sends the data to both readers.
Dalzhima at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 3

> You'll need an intermediate buffer which reads from

> your InputStream and then sends the data to both

> readers.

even i thought of idea which u r saying...

but how do i do that..

do u mean , a bufferedReader in main method reads actual input and writes it to both the threads

and there need to b a different Bufferedreader in each thread which reads the

input from main method ?

manishmulani@nitka at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 4
read data once in a loop and pass it on to both the threads..instead of reading data seperately in both the threads..
2713a at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 5

You create a new class which uses whatever reader you wish to get the data from your InputStream. Then you have many options.. You can use an EventListener mechanism to notify any amount of Observers that some new piece of data has been read and then send this data in the Event object. This way you can register both of your threads for these events and you'll receive all the data that was read in both places.

Dalzhima at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 6

> You create a new class which uses whatever reader you

> wish to get the data from your InputStream. Then you

> have many options.. You can use an EventListener

> mechanism to notify any amount of Observers that some

> new piece of data has been read and then send this

> data in the Event object. This way you can register

> both of your threads for these events and you'll

> receive all the data that was read in both places.

hey i've a doubt dont know if it really makes sense...

for implementing a listener there has to b a component right?... but there is no component concerned here..

i've encountered many listeners but have always implemented from a component.. so how do i achieve this here..

manishmulani@nitka at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 7
pls reply
manishmulani@nitka at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 8
What do you mean a Component?No there isn't any prerequisite with any Graphical Component to implement the [url= http://en.wikipedia.org/wiki/Observer_pattern]Observer Design Pattern[/url].
Dalzhima at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 9
Next time please use a meaningful subject line. Yours does not convey any information about the nature of your problem.
jverda at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...
# 10
@jverd: ok.... sorry for this time.. will take care frm next time@dalzhim: thanx a lot...
manishmulani@nitka at 2007-7-12 19:14:20 > top of Java-index,Java Essentials,Java Programming...