Code optimization and synchronized block
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, August 8, 2002 09:08 AM

Hi folks. I'm very interested in java code optimization and threading-issue. I think that following code optimization can occur in current implementation of JVMs and/or future implementation of JVMs. I mean java-compiler/hotspot optimizing here. 1) reordering synchronized ( o1 ) { data.y = 1; data.x = 1; } synchronized ( o2 ) { data.i = x; data.j = y; } 2) lock-merging synchronized ( obj ) { data.y = 1; synchronized ( obj2 ) { data.x = 1; } } synchronized ( obj ) { synchronized ( obj2   More>>

Hi folks.


I'm very interested in java code optimization and threading-issue.


I think that following code optimization can occur in current implementation
of JVMs and/or future implementation of JVMs.


I mean java-compiler/hotspot optimizing here.


			
1) reordering

synchronized ( o1 ) {
data.y = 1;
data.x = 1;
}


synchronized ( o2 ) {
data.i = x;
data.j = y;
}


2) lock-merging


synchronized ( obj ) {
data.y = 1;
synchronized ( obj2 ) {
data.x = 1;
}
}


synchronized ( obj ) {
synchronized ( obj2 ) {
data.x = 1;
data.y = 1;
}
}

3) lock-coarsing

synchronized ( obj ) {
data.y = 1;
}

synchronized ( obj ) {
data.x = 1;
}


synchronized ( obj ) {
data.y = 1;
data.x = 1;
}


4) lock-coarsing #2

synchronized ( obj ) {
data.y = 1;
}

synchronized ( obj2 ) {
data.x = 1;
}


synchronized ( obj ) {
synchronized ( obj2 ) {
data.y = 1;
data.x = 1;
}




Am I right?
Or is there anything wrong?



TIA.



Sincerely,
Min-Koo Seo.

   <<Less

Re: Code optimization and synchronized block

Posted By:   Zac_Jacobson  
Posted On:   Thursday, August 8, 2002 12:26 PM

Is #1 what you meant to say? It doesn't look like your original code does the same thing as the optimized.

The optimization may not perform the same way in the two instances of #2 or #4, it depends where else you synchronize on these objects. For 2 threads A and B, you can have a deadlock with these if objA==obj2B and objB==obj2A

#3 looks good.

About | Sitemap | Contact