473,657 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.fiel d += 10;
}
}

class Worker2 extends Thread
{
ImportantMethod ()
{
// do some more work on a class instance "shared data"
SharedData.fiel d -= 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 1553
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
907
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 in the file change very rarely, so I would like to keep it in a single copy in-memory if that's possible.
5
1699
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 released as needed, runs fine for some time, then stops - Visual Studio detects no exceptions, and I have placed break points on all the handling code to catch them. No errors / failures are logged, but child threads have all exited, and I am left...
2
1148
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: m_totalThreads=Convert.ToInt32(Settings.GetSetting("ProcessorThreads")); m_executingThreads=new Thread; for (int i=0;i < m_totalThreads;i++) {
3
2790
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 IDs. Thanks Vivek
14
2111
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 server. I've been told time and time again that I should use an event driven server design (that is, use twisted). There is a lot of interaction between the clients and they would often need to write to the same list of values, which of course...
26
5354
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
6
6913
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 user actions, I want to create n number instances of those classes, which is obviously unknown at compile time. What is the easiest way to create and then manage these kinds of objects? Using an array seems intuitive but it also seems like it...
5
1470
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 words, I can do this: .... pass .... 33
12
11073
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.