Hello there.
I'm kinda new to Java, did some last year and have forgotten
it all, but am experienced enough with C++. I have a
problem here that I would imagine is solved very easily.
I have a class that contains a main function:
// pseudo-code: please don't condemn the syntax or
// missing keywords
class MainOne
{
main()
{
// create two instances of the worker
// classes each, which are both threads.
createWorker1();createWorker1();
createWorker2();createWorker2();
}
}
class Worker1 extends Thread
{
ImportantMethod()
{
// do some work on a class instance "shared data"
SharedData.field += 10;
}
}
class Worker2 extends Thread
{
ImportantMethod()
{
// do some more work on a class instance "shared data"
SharedData.field -= 5;
}
}
As long as synchronization is possible with a given
solution, that can be disregarded at this point as I am
confident I can figure that part out (or if not I'll post
here <grin>). At this stage, my problem is how to create a
class instance that both of these classes can access and
change. There are no globals in Java right? So, what is
the "proper" way of doing it? Worker1 and Worker2 (any
thread instance of them) both need to be able to access this
one variable (class instance).
Thanks a bunch,
Mike