| | |
Synchronized Multidimensional Array
Thread 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 4: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?
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows





