Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot reproduce" - is Java deterministic multithreading possible?

Is this possible to run multithreaded Java application in a deterministic fashion? I mean to have always the same thread switching in two different runs of my application.

Reason for that is to run simulation in exactly the same conditions in every run.

Similar case is when one gives some arbitrary seed when using random number generator to obtain always the same "random" sequence.

like image 751
sZpak Avatar asked Apr 14 '16 15:04

sZpak


2 Answers

I am not aware of any practical way to do this.

In theory, it would be possible to implement a bytecode interpreter with an entirely deterministic behavior under certain assumptions1. You would need to simulate the multiple threads by implementing the threads and the thread scheduling entirely in software and using a single native thread.


1 - For example, no I/O, and no use of the system clock.

like image 118
Stephen C Avatar answered Sep 30 '22 04:09

Stephen C


No it is not possible (other than to simulate it yourself) to use multiple threads interleaving in the same way each time around. Threads are not designed to do that.

If you want deterministic results, don't use threads.

like image 41
OldCurmudgeon Avatar answered Sep 30 '22 05:09

OldCurmudgeon