473,671 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"shared" object

Hello

I want to create a kind of shared object. I have created a class,
Counter, which you can then import. Through the Counter class, i would
like to have access to a shared object.

Lets say that Counter contains only an int, initialised to 1. Each
time a class importing Counter would do Counter.getNext Number(), the
getNextNumber() method would increase the local int, and return the
next number.

What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from
1, but keeps counting from where another class left off.

I thought of maybe just writing the number to a file, and updating it
each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen
Dec 19 '05 #1
7 7346
In article <43************ ***********@dre ader2.cybercity .dk>, "Carsten H. Pedersen" <no@nono.no> wrote:
Hello

I want to create a kind of shared object. I have created a class,
Counter, which you can then import. Through the Counter class, i would
like to have access to a shared object.

Lets say that Counter contains only an int, initialised to 1. Each
time a class importing Counter would do Counter.getNext Number(), the
getNextNumber( ) method would increase the local int, and return the
next number.

What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from
1, but keeps counting from where another class left off.

I thought of maybe just writing the number to a file, and updating it
each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen


Make the counter int static. Assuming this is a simple POJO, it should work.
(All accesses to the class are using the same JVM.)
Dec 20 '05 #2
Hi Carsten.

public class Counter extends .... {
public Counter() {
super(...);
}

private void updateCounter() {
count += 1;
}

public static int count = 1;
}

class AnotherClass extends ... {
public AnotherClass()
super(...);
}

Counter counter = new Counter();
counter.updateC ounter();

This should the job.

Warm Regards
Darko Topolsek

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader2.cyber city.dk...
Hello

I want to create a kind of shared object. I have created a class, Counter,
which you can then import. Through the Counter class, i would like to have
access to a shared object.

Lets say that Counter contains only an int, initialised to 1. Each time a
class importing Counter would do Counter.getNext Number(), the
getNextNumber() method would increase the local int, and return the next
number.

What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from 1,
but keeps counting from where another class left off.

I thought of maybe just writing the number to a file, and updating it each
time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen

Dec 20 '05 #3
Darko Topolsek wrote:
public class Counter extends .... {
public Counter() {
super(...);
}

private void updateCounter() {
count += 1;
} Shouldn't this be public? How else can it be accessed from the
outside? Or did i miss some trick? :)
[...]


Thanks for responding, but this doesn't quite do the trick. I think i
have to elaborate on what i want. From what i wrote previously, i
don't think it came out clearly.

I have a package, foo, containing the Counter class. Now i have a
class, A, which does: import foo.Counter;

A has a main method, which starts an instance of itself, wrapped in a
Thread. The Thread works as a ticker, counting the shared object
Counter up one.

Now i start another instance of A, from another shell. Since this does
the exact same thiing as the first A, it also imports foo.Counter.
What i want is for the second instance to start counting from where
the first instance was at when the second was started.

An example printout would be like this:

(Start A1, first instance of A, with java A)
A1: count: 1
A1: count: 2
A1: count: 3
(start A2, second instance of B, in another shell, with java A)
A2: count: 4
A1: count: 5
A2: count: 6
A1: count: 7

And so on...

From what i tried, A2 just starts counting from 1, and A1 just keeps
counting.

Any suggestions on how to crack this one? :)

- Carsten H. Pedersen
Dec 21 '05 #4

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader2.cyber city.dk...
Hello

I want to create a kind of shared object. I have created a class, Counter,
which you can then import. Through the Counter class, i would like to have
access to a shared object.

Lets say that Counter contains only an int, initialised to 1. Each time a
class importing Counter would do Counter.getNext Number(), the
getNextNumber() method would increase the local int, and return the next
number.

What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from 1,
but keeps counting from where another class left off.

I thought of maybe just writing the number to a file, and updating it each
time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen


Stay away from static instances/variables, in spite of the earlier
responses. Although this is always a good idea the fact that you want
separate sequences implies that you should go for multiple insatnces of
sequence generators. If you could construct such a generator with an initial
sequence value (as opposed to 1) it would be easy to spawn sequences.

Silvio Bierman
Dec 21 '05 #5

"Carsten H. Pedersen" <no@nono.no> wrote in message
news:43******** *************** @dreader1.cyber city.dk...

Thanks for responding, but this doesn't quite do the trick. I think i have
to elaborate on what i want. From what i wrote previously, i don't think
it came out clearly.

I have a package, foo, containing the Counter class. Now i have a class,
A, which does: import foo.Counter;

A has a main method, which starts an instance of itself, wrapped in a
Thread. The Thread works as a ticker, counting the shared object Counter
up one.

Now i start another instance of A, from another shell. Since this does the
exact same thiing as the first A, it also imports foo.Counter. What i want
is for the second instance to start counting from where the first instance
was at when the second was started.

An example printout would be like this:

(Start A1, first instance of A, with java A)
A1: count: 1
A1: count: 2
A1: count: 3
(start A2, second instance of B, in another shell, with java A)
A2: count: 4
A1: count: 5
A2: count: 6
A1: count: 7

And so on...

From what i tried, A2 just starts counting from 1, and A1 just keeps
counting.

Any suggestions on how to crack this one? :)


Your example is completely different from the explanation of your first
post, and completely different from your explanation in this post. I will
ignore both explanations and assume what you want is what the example shows.

In that case, you want to use the Singleton design pattern.

public class SingletonCounte r {
private static final SingletonCounte r soleInstance = new
SingletonCounte r();

private SingleCounter() {
//Private constructor to prevent instantiation.
}

private int value = 1;

public getNextValue() {
return this.value++;
}

public static SingletonCounte r getInstance() {
return soleInstance;
}
}

- Oliver
Dec 23 '05 #6
EricF wrote:
In article <43************ ***********@dre ader2.cybercity .dk>, "Carsten H. Pedersen" <no@nono.no> wrote:
Hello

I want to create a kind of shared object. I have created a class,
Counter, which you can then import. Through the Counter class, i would
like to have access to a shared object.

Lets say that Counter contains only an int, initialised to 1. Each
time a class importing Counter would do Counter.getNext Number(), the
getNextNumber() method would increase the local int, and return the
next number.

What i want now is when an entirely different class, also importing
Counter, calls the getNextNumber() method, it does not start out from
1, but keeps counting from where another class left off.

I thought of maybe just writing the number to a file, and updating it
each time getNextNumber() was called. Is there an easier way?
- Carsten H. Pedersen


Make the counter int static. Assuming this is a simple POJO, it should work.
(All accesses to the class are using the same JVM.)


This would have worked if you were using the same JVM as EricF
suggested. A later message mentions that you are creating a second
object in a second shell indicating to me that you are using two JVM
instances. This approach doesn't work because each JVM instance has its
own environment and they are not shared.

What you want is that when a second "object" is "created" it really
accesses the first object created regardless of whether that happens in
the same JVM or not. What about if you create a Counter object in a
second computer all together? Should it connect to the first Counter
object created (on some other unknown computer)?

You are trying to solve a very hard problem here. As ugly a solution as
using the file system as memory might be the easiest solution for single
computer processing. Make sure you use appropriate locking mechanisms
since two JVM processes (and hence two instances of Counter) could try
to access the file at the same time.

You also don't know when the Counter object "dies". Is the Counter alive
only when there are instances of it alive or is it alive regardless? You
have a lot of things to think about here.

RMI, Beans, CORBA, COM, .NET, web services, etc. all exists as
frameworks to help formulate solutions to this very hard problem. Most
of these (if not all) will probably be too complex for what you are
doing. Then again, locking access to a file is hard as well.

--
Edwin
Dec 26 '05 #7
Edwin Castro wrote:
RMI, Beans, CORBA, COM, .NET, web services, etc. all exists as
frameworks to help formulate solutions to this very hard problem. Most
of these (if not all) will probably be too complex for what you are
doing. Then again, locking access to a file is hard as well.

I'm looking into RMI now, and it seems to be able to do exactly what i
want. Thanks for the pointer. :)
Dec 28 '05 #8

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

Similar topics

3
2026
by: Pro Grammer | last post by:
Hello, all, I am not sure if this is the right place to ask, but could you kindly tell me how to "load" a shared object (like libx.so) into python, so that the methods in the .so can be used? That too, given that the shared object was written in c++, compiled with g++ ? Thanks, Pro Grammer
2
2032
by: santa19992000 | last post by:
Confusing th eword with "library", "shared library" and how to use these things in real C project, is there any small example I can take a look. Thanks.
2
1623
by: Jenna Schmidt | last post by:
I know that one of the benefits of using "Shared" methods is you do not explicitly have to Dim as New object to access the method. Are there some other implications with memory and concurrency issues with lets say 25-50 concurrent users? What are the pros and cons of using this type of method? Thanks so much,
6
1446
by: Ross | last post by:
MyWebProject.MyWebForm1.someset.somedata is a datatable within a dataset. Displays quite nicely, too. Now I want to use the same data in MyWebProject.MyWebForm2. Being a old, er, experienced Java programmer, I thought I could write this as a member of MyWebForm1: Public Shared Function getSomeData() As DataTable getSomeData = somedata End Function
3
1525
by: Henri | last post by:
Hi, Sorry for my bad English. Could you tell me if the context of a member declared as Shared is limited to the instance of the page the object that owns this member is declared in, or does it go to the whole application? I mean: if one user accesses a page that updates the shared member of this object, will it affect all the users of the application or not? Thanks!
1
6780
by: David Sanschagrin | last post by:
(I previously posted this problem on vb.general.discussion but I've been told that this question is more related to VB.NET than VB6 and so that I should post that here.) I'm trying to call a "Public Shared" function in a dll built in VB.NET from a VB6 program. Whenever I try, I get an error saying that the function doesnt exist. I had doubts that the "Shared" in the VB.NET dll could be the problem. I tried to
2
1554
by: HmFireBall | last post by:
Hi, This is a question about a shared property in ASP.NET Imagine there's an assembly called my.dll that is in the /bin directory of several websites. This assembly contains a class with a shared property called MySharedProp. Its value is initialized inside the shared constructor of the class and should be based on values declared in the web.config of
3
19278
by: Kenneth Kahl | last post by:
Hello, I would like to call a C++ programm out of Java with help of JNI. By the followed command I created a "shared library": g++ -shared -o libcalculate.so rechner.cpp When I create an object from the existing program inside a method of my class rechner.cpp, and then call the method out of java, a following
3
2259
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on WebForm2.aspx. For declaring the property oRpte() on WebForm1.aspx, I use "Public Property" and I declare variable _oRpte as Friend Shared. That's my problem. If I don't declare _oRpte as Friend Shared, I can't use WebForm1.oRpte() on other webpage. If I declare _oRpte as Friend Shared, I can use...
0
8907
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
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
8593
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
7423
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
6218
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
5687
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
4215
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...
1
2804
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
2
2046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.