Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Will synchronizing really have an impact in this case?  (Read 917 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2011-12-30 12:55:15 »

I want to modify a large number of ArrayLists from multiple threads. Not very many of the ArrayLists are modified per thread so the chance of two threads modifying the same list is very small.

Consider this example:

Thread 1:
1  
2  
3  
synchronized(listA){
    //Modify listA
}


Thread 2:

1  
2  
3  
synchronized(listB){
    //Modify listB
}


Would the synchronization have any performance impact what so ever in this case? I believe it shouldn't.

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #1 - Posted 2011-12-30 14:55:04 »

Synchronization is the heaviest operation in Java. It can take thousands of CPU cycles to lock on an object and pretty much disables aggresive out-of-order execution of instructions, as the JVM has to guarantee X happened before Y.

There are however optimisations in the HotSpot JVM that can replace a 'monitor enter' with a busy-loop, or even verifying that locks are never accessed concurrently, in which case the sync can be optimized away. Once the JVM determines that a lock is contented, it will rollback these optimisations.

Having said all that, the performance overhead can be negligible, depending on how much work you actually perform within the synchronized block. So as always, it depends. Benchmark it in a realworld scenario and draw conclusions from that.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline princec
« League of Dukes »

JGO Kernel


Medals: 196
Projects: 3


Eh? Who? What? ... Me?


« Reply #2 - Posted 2011-12-30 16:57:54 »

Very hard to benchmark in a real-world scenario unless you've got a whole pile of real-world hardware configurations and operating systems to play with.

Cas Smiley

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2011-12-30 17:54:41 »

I think it's simply key to get a lot done in your sync-block, so that no matter how big the overhead, it becomes negligible.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Online sproingie
« Reply #4 - Posted 2011-12-31 03:52:56 »

Doing lots of work inside the synchronized block is the best way to make sure your locks become bottlenecks or cause outright deadlock.  The fastest solution is not sharing the lists between threads in the first place.  Hard to say if that's possible from the description.


Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #5 - Posted 2011-12-31 03:54:55 »

Doing lots of work inside the synchronized block is the best way to make sure your locks become bottlenecks or cause outright deadlock.  The fastest solution is not sharing the lists between threads in the first place.  Hard to say if that's possible from the description.

and then there is this thing called context:

Not very many of the ArrayLists are modified per thread so the chance of two threads modifying the same list is very small.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline delt0r

JGO Coder


Medals: 18


Computers can do that?


« Reply #6 - Posted 2011-12-31 11:09:02 »

I would recommend using locks from java.util.concurrent. In all my tests they are never slower than synchronized and sometimes are faster because you have more control. The biggest gains can come from cases where there are many reads and less often writes... and its readWriteLock for the win. I have seen great performance using that on some fluid simulation stuff where many threads work on common data structures.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline theagentd
« Reply #7 - Posted 2011-12-31 11:39:23 »

In my case, I have two solutions to my problem. One is to allow the threads to modify the lists, and one is to defer the changes by saving them and execute them single-threaded after everything. I think it will be faster to use synchronization since I will not be doing many changes per update, like 80% of the time it will be 0 changes but sometimes several in a single frame. The chance of two lists being modified in the same frame is extremely low, but if synchronization is as expensive as you say...

Concerning how much work I actually do in each change, I realized that I might have to keep the lists sorted, since the order of the objects in them may have an effect if the scripts used in the game are order-dependent (I need determinism for network synchronization and replaying). I might therefore have to switch from ArrayList to some kind of sorted list or set, which would of course take more time to change...

In the end, I guess only an actual benchmark would be able to give me an answer. Thanks for all the quick responses!

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (71 views)
2013-05-17 21:29:12

alaslipknot (82 views)
2013-05-16 21:24:48

gouessej (112 views)
2013-05-16 00:53:38

gouessej (107 views)
2013-05-16 00:17:58

theagentd (118 views)
2013-05-15 15:01:13

theagentd (107 views)
2013-05-15 15:00:54

StreetDoggy (151 views)
2013-05-14 15:56:26

kutucuk (175 views)
2013-05-12 17:10:36

kutucuk (172 views)
2013-05-12 15:36:09

UnluckyDevil (181 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.136 seconds with 21 queries.