Threading Issue ..

  • Thread starter Edward Leibnitz
  • Start date
E

Edward Leibnitz

I want to find out If you can pass a declared object either final or
otherwise out of a anonymous inner class and back to the calling thread

A and B go in as a final .... how do you get C back so that you can use it

(don't concern yourself with the thread scheduling issues ...... just the
variable syntax)


public static void main(String[] args)
{

final int A = 6;
final int B = 12;
int D;

....... etc, etc ......

new Thread(new Runnable()
//anonymous inner class that runs a thread!
{

public void run()
{
C = A + B;

}
}).start();


D = A + B + C;

....... etc, etc ......


}


Any help would be appreciated!!!!


-Ed
 
C

Chris Smith

Edward said:
public static void main(String[] args)
{

final int A = 6;
final int B = 12;
int D;

....... etc, etc ......

new Thread(new Runnable()
//anonymous inner class that runs a thread!
{

public void run()
{
C = A + B;

}
}).start();


D = A + B + C;

....... etc, etc ......


}

No, you can't. What you can do is use a named local inner class to hold
the value, which will allow you to get it back out. So, instead of the
above, use:

public static void main(String[] args)
{
final int a = 6;
final int b = 12;

class AdditionTask implements Runnable
{
int c;

public void run()
{
c = a + b;
}
}

AdditionTask task = new AdditionTask();
new Thread(task).start();

// do something to ensure the thread has actually set the value.

int d = a + b + task.c;
}

The problem is that you need to add a new API element, and you can't
really do that in a useful way with an anonymous inner class, so you use
a named one instead.

Of course, this will get more complex with synchronization, but you said
to omit that, so I did.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Edward Leibnitz

Thanks for the help but ...


//***************************************
import java.io.*;
import java.util.*;

public class TestThread
{
public static void main(String[] args)
{

final int a = 6;
final int b = 12;

int d;

class AdditionTask implements Runnable
//named inner class that can return values to caller?
{
int c;

public void run()
{
c = a + b;
}
}

AdditionTask task = new AdditionTask();
new Thread(task).start();

d = a + b + c;

}
}

//***************************************


This doesn't work.

it compiles when the line d = a+b+c; is commented out however!


-Ed
 
E

Edward Leibnitz

and it doesn't work for ....

//***********************************************
class AdditionTask implements Runnable
//named inner class that can return values to caller?
{
int c = 0; <- just in case early initialization might have
been a problem

public void run()
{
c = a + b;


//***********************************************

either

Thanks for any help! honest ....

-Ed
 
C

Chris Smith

Edward said:
Thanks for the help but ...

[... snip code ...]

This doesn't work.

it compiles when the line d = a+b+c; is commented out however!

If you'll refer to my response to you, that line read as follows
instead:

d = a + b + task.c;

The whole point is that if c is a local variable, then the other thread
can't modify it; so for the other thread to be able to write to it, it
has to be made a field of the Runnable implementation, which can then be
made into shared state and written/read equally by either side.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Edward Leibnitz

thanks so much .....

If you'll refer to my response to you, that line read as follows
instead:

d = a + b + task.c;

The whole point is that if c is a local variable, then the other thread
can't modify it; so for the other thread to be able to write to it, it
has to be made a field of the Runnable implementation, which can then be
made into shared state and written/read equally by either side.


oh yes ... that definitely makes sense to me now ...

-Ed
 

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

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top