please go through the code

i have an problem with interthread communication see the code below:

Two threads are independent thread and i would like to access the variable from another thread.

I have written two thread class one will keep on update the vector table and sleep for a specified time. And another class has to get the vector variable and get the message from the buffer and put it to the file and delete all the message from the buffer. If any one know the interthread communication pls do correct the programme urgent. bye

import java.io.*;

import java.util.*;

import java.lang.reflect.*;

class AddVector extends Thread

{

Vector buffer = new Vector();

ThreadGroup threadGroup;

String str;

String refNo = new String();

String message = new String();

String api_Name = new String();

String station_Name = new String();

String status = new String();

String date_Time = new String();

String token = new String();

public AddVector (ThreadGroup threadGroup, String str) {

super(threadGroup, str);

this.str = str;

start();

}

public void addVector() {

Vector v = new Vector();

String refNo = "123";

String message = "Testing Message";

String api_Name = "OneApi";

String station_Name = "station process";

String status = "Norm";

String date_Time = "14/08/2001";

String token = "future";

v.addElement( refNo );

v.addElement( message );

v.addElement( api_Name );

v.addElement( station_Name );

v.addElement( status );

v.addElement( date_Time );

v.addElement( token );

buffer.addElement(v);

System.out.println("buffer size :"+buffer.size());

}

public void run () {

for (int i= 0;i < 1;i++ ) {

for(int j = 0; j < 20; j++) {

System.out.println("Thread Name:"+getName());

addVector ();

}

try{

sleep(3000);

}

catch (Exception e) {

System.out.println("Error :"+e);

}

}

}

}

class WriteFile extends Thread

{

String str ;

ThreadGroup threadGroup;

public WriteFile (ThreadGroup threadGroup, String str) {

super ( threadGroup, str );

this.str = str;

System.out.println("with in writefile const");

start();

}

public void run () {

try

{

if (threadGroup != null) {

Thread[] threads = new Thread ( threadGroup.activeCount() );

threadGroup.enumerate ( threads );

for ( int i = 0; i< threads.length; i++ )

if ( threads != null && threads != Thread.currentThread()){

if (threads.isAlive()) {

if ( "addtovector".equalsIgnoreCase ( threads.getName()) ) {

System.out.println( threads.buffer.size() );

}

}

}

}

}

catch (Exception e)

{

System.out.println("with in catch :"+e);

}

}

}

public class LogManager

{

public static void main ( String args[] ) {

ThreadGroup threadGroup = new ThreadGroup("jeevan");

new AddVector(threadGroup,"addtovector");

new WriteFile(threadGroup,"writefile");

}

}

[3294 byte] By [jeevankc] at [2007-9-26 4:08:31]
# 1

Hi,

PipedInputInputStream p1=new PipedInputStream();

PipedOutputStream p2=new PipedOutputStream(p1);

ObjectInputStream in=new ObjectInputStream(p1);

ObjectOutputStream out=new ObjectOutputStream(p2);

Now pass the "in" to the thread which requires data from the other thread and "out" to the other thread.

Now write the Vector you have prepared using out.writeObject(vectorObj);

At the other thread just read,

Vector fromOther=(Vector)in.readObject();

Isnt this what you want, Hope this be of some help

regards Stallon

stallon at 2007-6-29 13:10:46 > top of Java-index,Archived Forums,Java Programming...