Arrays in Java - accessed by 2 objects.

Associate
Joined
30 Oct 2004
Posts
112
Location
Whitstable
Hey everyone,

I'm writing a program, in which a 2D array is being used. Basically, I have 2 other objects which update this array, and I want them to be able to update it at the same time.

Is this possible? I am thinking it is gonna be a 'concurrency' issue, but wondered if it was possible outside of doing this?

Cheers!
 
You can have two threads update an object "at the same time". Just use proper synchronization (locks/monitors) and it will be fine.

There really is no other way round it if you want proper concurrent access, any other methods could leave your array in an inconsistent state (Race conditions etc).
 
Last edited:
retrac2k said:
Hey everyone,

I'm writing a program, in which a 2D array is being used. Basically, I have 2 other objects which update this array, and I want them to be able to update it at the same time.

Is this possible? I am thinking it is gonna be a 'concurrency' issue, but wondered if it was possible outside of doing this?

Cheers!

You sound a little confused. If you have two objects updating the same array from a single thread then this is absolutely fine (always) - there is no concurrency issue.

If the two objects updating the array are updating it from seperate threads then there are potential concurrency issues. Nothing is going to 'break' (you wont get any exceptions or anything), so unless you have a particular reason for enforcing that both threads cant access the array at once then there is no problem with allowing concurrent access.
 
Last edited:
OK, so if they are both running in the same thread, then there is no problem?

My task is basically to create a game that runs over a network, and I'm using a 2D array as a 'grid', to represent the game board. Both objects have to be able to update at the same time over the network.

Its a uni assignment, and its doing my head in to say the least!
 
If they are both running in the same thread of execution there is indeed no problem.
 
Are they not running on seperate hosts, and therefore in different virtual machines, and different threads.....

If that above is true then you need some sort of lock mechanism to only allow one thread to access the game board, i think just making the method synchronized should work.

Code:
public synchronized someMethod()
 
Yep, they will be running on different hosts.

So if I just make the 'update' method synchronised, i shouldn't have any problems?
 
No offence but you dont really seem to have a clue what you are doing. I suggest you read up about what 'problems' can arise due to multithreaded access to a shared data structure, since I dont believe you know what they actually are?!

If its just an array being updated and you want them to both be able to update the board at the same time, then there is not necessarily any need for synchronization even if they are running from seperate threads. It depends completely on the nature of the updates and the rules of the game (e.g: does ordering matter, are there issues about whether a game board is consistent, are the updates to a single value in the array or multiple values etc etc etc)
 
Lagz said:
No offence but you dont really seem to have a clue what you are doing.

Hmmm, well, if everyone knew the answer to everything, then I guess there wouldn't be any need for this forum!

This is the exact reason I asked, so maybe these comments aren't neccessary??
 
If you're using Servlets to interrogate the arrays held as the gameboard then it might be worth having a look at the SingleThreadServlet model as a way of handling synchronization (in addition to looking at synchronization). From reading it sounds like there are scaleability issues but it could be worth a look.

Never actually used this, so I don't know all the issues involved, or if it indeed does what I think it does.

Hope that helps anyway.

Jim
 
is it me, or shouldnt you make sure that you cannot access the array at the same time, but one pc access the gameboard after the other to ensure data integrity?
 
Design your server(lets) interface to interact with the array, don't use direct interaction between hosts.

From this, you can then maintain the array with a single thread to avoid concurrency. Think of it like a webserver - each client will send information to the server, the server will then perform the updates.

That's very, very summarised, but should get you started.
 
Excellent, many thanks for all of your replys everyone! Hopefully I should be able to get some kind of model running!

Cheers! :)
 
Back
Top Bottom