Getting value of variable from Thread to main class  
Author Message
adamromanski





PostPosted: 2004-4-16 19:21:00 Top

java-programmer, Getting value of variable from Thread to main class I have a problem, with getting value of a variable that is set in
thread. I start the Thread in a main class, thread gets the value from
server and sets it as static variable. After starting the thread I try
to print the value of " threadObjetc.staticvariable " but every time
it has a null value. I suppose that the reason for that is that thread
works in a background and functions of main class continues anyway. If
someone had a similar problem and could help me to avoid it I would be
gratefull for help, Thanks
 
dlm8751





PostPosted: 2004-4-17 1:16:00 Top

java-programmer >> Getting value of variable from Thread to main class You probably need to wait for the thread to complete.

Thread myThread = new MyThread();

myThread.start();
myThread.join();

there is an exception you will have to catch regarding join().

The effect is that the main will wait until myThread has completed
before the call to join() will return. So, you can start your thread,
go off and do other work in main and then tell the VM that you are
ready for the results from myThread.


"Bryce (Work)" <email***@***.com> wrote in message news:<email***@***.com>...
> On 16 Apr 2004 04:21:14 -0700, email***@***.com (Adam Romanski)
> wrote:
>
> >I have a problem, with getting value of a variable that is set in
> >thread. I start the Thread in a main class, thread gets the value from
> >server and sets it as static variable. After starting the thread I try
> >to print the value of " threadObjetc.staticvariable " but every time
> >it has a null value. I suppose that the reason for that is that thread
> >works in a background and functions of main class continues anyway. If
> >someone had a similar problem and could help me to avoid it I would be
> >gratefull for help, Thanks
>
> are you setting the variable in the thread?
>
> Is it possible that when you try to read the variable, it hasn't been
> set yet?