•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 397,810 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 2,517 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: 1466 | Replies: 1 | Solved
![]() |
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:
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.
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?
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!
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?
How many people have code in their Sigs?
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
Results of Test 1
Test 2
Results of Test 2
:cheesy:
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)
public class TestingMain { public static void main(String[] args) { new TestingMain(); } // ========================= Object[][] myArray = new Object[256][0]; public TestingMain() { System.out.println("Starting thread..."); // Initialize a thread that will share the resource new TestThread().start(); System.out.println("Thread started."); try { System.out.println("Main going to sleep for 1 second."); Thread.sleep(1000); System.out.println("Main done sleeping."); } catch (Exception ex) { System.out.println("Main: " + ex); } synchronized (myArray) { System.out.println ("And we now have access to myArray!"); } System.exit(0); } class TestThread extends Thread { public void run() { synchronized (myArray) { // Lock myArray for FIVE seconds! try { System.out.println("Thread going to sleep for 5 seconds."); this.sleep(5000); System.out.println("Thread done sleeping."); } catch (Exception ex) { System.out.println("Thread: " + ex); } } } } }
Results of Test 1
run:Great! The lock worked just as it should have worked. Now, let's be more specific:
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!
Test 2
java Syntax (Toggle Plain Text)
public class TestingMain { public static void main(String[] args) { new TestingMain(); } // ========================= Object[][] myArray = new Object[256][0]; public TestingMain() { System.out.println("Starting thread..."); // Initialize a thread that will share the resource new TestThread().start(); System.out.println("Thread started."); try { System.out.println("Main going to sleep for 1 second."); Thread.sleep(1000); System.out.println("Main done sleeping."); } catch (Exception ex) { System.out.println("Main: " + ex); } synchronized (myArray[4]) { System.out.println ("And we now have access to myArray[4]!"); } synchronized (myArray[123]) { System.out.println ("And we now have access to myArray[123]!"); } System.exit(0); } class TestThread extends Thread { public void run() { synchronized (myArray[123]) { // Lock myArray[123] for FIVE seconds! try { System.out.println("Thread going to sleep for 5 seconds."); this.sleep(5000); System.out.println("Thread done sleeping."); } catch (Exception ex) { System.out.println("Thread: " + ex); } } } } }
Results of Test 2
run: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!
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]!
: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?
How many people have code in their Sigs?
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
- Multidimensional array sort problem (C++)
- Question regarding multidimensional array for navigation bar (Graphics and Multimedia)
- Sort multidimensional array on more than one column? (ASP)
- multidimensional array merge using PHP (PHP)
- How to Sort a MultiDimensional Array (C)
- multidimensional array (C)
- Java Multidimensional Arrays (Java)
Other Threads in the Java Forum
- Previous Thread: Hellpp!!!
- Next Thread: Task handling tool in Table of Swing Application


Linear Mode