473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

threaded instances of two classes accessing the same variable

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
Jul 17 '05 #1
1 1546
Mike Frayn wrote:

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).


Just like you would do in C++ since you don't use globals there either,
right? :)

In any case, I would pass the object to the constructor of each of the
workers and save it in an instance variable. Look into the synchronize
keyword for help with synchronization.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Angelos Karantzalis | last post by:
Hi y'all ... I'm a bit puzzled here about .NET class instancing under COM+ Issue 1: I've a COM+ component, let's call it ... COMDbWrapper that initializes itself from an xml file. The data...
5
by: Rob Durant | last post by:
Hi, I have a multi-threaded application (have also tried as service - same behaviour) that runs fine on XP, but not on 2003. Symptoms are: Threads are started normally, locks acquired and...
2
by: Robert May | last post by:
I have a windows service that spawns multiple threads running a thing that does a bunch of processing. Here's the code that spawns the threads: ...
3
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different...
14
by: Snor | last post by:
I'm attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
6
by: Bugs | last post by:
Does anyone have any recommendations on the best way to create multiple instances of the same class when the final number of instances is unknown? For example, I have a class and based on some...
5
by: Joseph Barillari | last post by:
Hi python-list, I've just started using new-style classes and am a bit confused as to why I can't seem to alter methods with special names (__call__, etc.) of new-style class instances. In other...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.