User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 423,450 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,745 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1581 | Replies: 1 | Solved
Reply
Join Date: Nov 2005
Posts: 72
Reputation: Cudmore is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Question Synchronized Multidimensional Array

  #1  
Feb 5th, 2007
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!
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Posts: 72
Reputation: Cudmore is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 5
Cudmore's Avatar
Cudmore Cudmore is offline Offline
Junior Poster in Training

Solution Alright!

  #2  
Feb 5th, 2007
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
  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
  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 3:40 pm. Reason: Simple changes
synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 2:48 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC