dcsimg
threads working with references and creating constructors
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   A_D
Posted On:   Thursday, March 16, 2006 12:52 PM

hey i've just created two threads "A" and "B" and i want pass a reference from "B" to "A" and vice-versa just like this: public class TwoThreadsWithReferences { public static void main (String argv [ ] ) { AThread A = null; BThread B = null; A = new AThread ( B ); B = new BThread ( A ); A.start(); B.start(); } } in this case in particular with the second instance is no problem because the referenece of A has been instanciated but in the first case the reference of B that im passing to "A" has null because "B" is instanciated in the following line the question is how can I pass the ref   More>>

hey i've just created two threads "A" and "B" and i want pass a reference from "B" to "A" and vice-versa just like this:
public class TwoThreadsWithReferences
{
public static void main (String argv [ ] )
{

AThread A = null;
BThread B = null;

A = new AThread ( B );
B = new BThread ( A );

A.start();
B.start();
}
}

in this case in particular with the second instance is no problem because the referenece of A has been instanciated but in the first case the reference of B that im passing to "A" has null because "B" is instanciated in the following line the question is how can I pass the references together avoidig a NullPointerException in the first case without reinstanciating "B" inside the constructor "A" like this?

A = new AThread ( B = new BThread ( A ));
it will be work but i need a more elegant solution perhaps making the references inside the threads in a Setter method for example and calling the "Setter" method in main before starting the threads??????? but i dont know how to solve this method example






public class AThread extends Thread
{
.....member variables


constructor
public AThread()
{
.....
}


public void run()
{



..................... code
}
public void Setter()
{
what should i put here??????????????????
}



any help ????????? many thanks in advance

   <<Less

Re: threads working with references and creating constructors

Posted By:   ankush_purwar  
Posted On:   Friday, March 17, 2006 02:45 PM

Try to use static keyword.
But I would say that it's not a good design that you are doing.I don't know your exact requirement. But try to think in this way, probably it would achieve the same thing that you want.
Make a common class C and instanitate it .
then create both threads using C'object.

Re: threads working with references and creating constructors

Posted By:   Edward_Harned  
Posted On:   Friday, March 17, 2006 12:27 PM

Format your code so people can read it.

About | Sitemap | Contact