943,809 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5786
  • Java RSS
Feb 5th, 2007
0

Synchronized Multidimensional Array

Expand Post »
My question today is regarding multidimensional arrays, and synchronization.. I'm working on a client/server project and I'm not sure about the theory of an idea I came up with:

I have an array:

Object[][] myArray = new Object[256][];

It is an array of arrays. The length of myArray is 256 and will never change from 256. However, the length of the second level of arrays is dynamic, and each second-level array can be null, zero-length, or any other length.

What I need to do, since multiple threads work with myArray simutaneously, is synchronize the array during possible changes to the second-level arrays. Anywhere an access/modification is made, myArrays is synchronized.

synchronized (myArray) { ... }

The problem is that synchronize causes threads to block, even when they wish to read or write to a different second-level array than the one already being processed. What I want to do is lock only the specific, second-level array, and leave the rest of the arrays unlocked so the program can function more quickly.

Can I do this?

synchronized (myArray[indexToLock]) { ... }

I can ensure that the second-level arrays are never null, rather zero-length or more. NetBeans lets me write the code, but to test it...

Any thoughts? Thank youuu!
Similar Threads
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005
Feb 5th, 2007
1

Alright!

Alright!

I love answering my own questions. I hope that someone can learn something from this!

I conducted two tests. The first test locks the entire myArray array. The seconds locks a specific, second-level array. Both tests were monitored by myself.

Test 1
java Syntax (Toggle Plain Text)
  1. public class TestingMain {
  2.  
  3. public static void main(String[] args) {
  4. new TestingMain();
  5. }
  6.  
  7. // =========================
  8.  
  9. Object[][] myArray = new Object[256][0];
  10.  
  11. public TestingMain() {
  12.  
  13. System.out.println("Starting thread...");
  14.  
  15. // Initialize a thread that will share the resource
  16. new TestThread().start();
  17.  
  18. System.out.println("Thread started.");
  19.  
  20. try {
  21. System.out.println("Main going to sleep for 1 second.");
  22. Thread.sleep(1000);
  23. System.out.println("Main done sleeping.");
  24. } catch (Exception ex) {
  25. System.out.println("Main: " + ex);
  26. }
  27.  
  28. synchronized (myArray) {
  29. System.out.println ("And we now have access to myArray!");
  30. }
  31.  
  32. System.exit(0);
  33.  
  34. }
  35.  
  36. class TestThread extends Thread {
  37. public void run() {
  38. synchronized (myArray) {
  39. // Lock myArray for FIVE seconds!
  40. try {
  41. System.out.println("Thread going to sleep for 5 seconds.");
  42. this.sleep(5000);
  43. System.out.println("Thread done sleeping.");
  44. } catch (Exception ex) {
  45. System.out.println("Thread: " + ex);
  46. }
  47. }
  48. }
  49. }
  50.  
  51. }

Results of Test 1
run:
Starting thread...
Thread started.
Main going to sleep for 1 second.
Thread going to sleep for 5 seconds.
(1 second passes)
Main done sleeping.
(4 seconds pass)
Thread done sleeping.
And we now have access to myArray!
Great! The lock worked just as it should have worked. Now, let's be more specific:

Test 2
java Syntax (Toggle Plain Text)
  1. public class TestingMain {
  2.  
  3. public static void main(String[] args) {
  4. new TestingMain();
  5. }
  6.  
  7. // =========================
  8.  
  9. Object[][] myArray = new Object[256][0];
  10.  
  11. public TestingMain() {
  12.  
  13. System.out.println("Starting thread...");
  14.  
  15. // Initialize a thread that will share the resource
  16. new TestThread().start();
  17.  
  18. System.out.println("Thread started.");
  19.  
  20. try {
  21. System.out.println("Main going to sleep for 1 second.");
  22. Thread.sleep(1000);
  23. System.out.println("Main done sleeping.");
  24. } catch (Exception ex) {
  25. System.out.println("Main: " + ex);
  26. }
  27.  
  28. synchronized (myArray[4]) {
  29. System.out.println ("And we now have access to myArray[4]!");
  30. }
  31.  
  32. synchronized (myArray[123]) {
  33. System.out.println ("And we now have access to myArray[123]!");
  34. }
  35.  
  36. System.exit(0);
  37.  
  38. }
  39.  
  40. class TestThread extends Thread {
  41. public void run() {
  42. synchronized (myArray[123]) {
  43. // Lock myArray[123] for FIVE seconds!
  44. try {
  45. System.out.println("Thread going to sleep for 5 seconds.");
  46. this.sleep(5000);
  47. System.out.println("Thread done sleeping.");
  48. } catch (Exception ex) {
  49. System.out.println("Thread: " + ex);
  50. }
  51. }
  52. }
  53. }
  54.  
  55. }

Results of Test 2
run:
Starting thread...
Thread going to sleep for 5 seconds.
Thread started.
Main going to sleep for 1 second.
(1 second passes)
Main done sleeping.
And we now have access to myArray[4]!
(4 seconds pass)
Thread done sleeping.
And we now have access to myArray[123]!
Excellent! From that I can see my program had immediate access to myArray[4], even though myArray[123] was locked by the second thread. To access myArray[123], however, the main thread had to wait until the object was unlocked. That's exactly what I wanted to know!

:cheesy:
Last edited by Cudmore; Feb 5th, 2007 at 4:40 pm. Reason: Simple changes
Reputation Points: 20
Solved Threads: 6
Junior Poster in Training
Cudmore is offline Offline
74 posts
since Nov 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Hellpp!!!
Next Thread in Java Forum Timeline: Task handling tool in Table of Swing Application





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC