Getting value of variable from Thread to main class

A

Adam Romanski

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
 
B

Bryce (Work)

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?
 
D

dmcreyno

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top