Execution order of multiple threads

The whole point of threads is that they can be executed concurrently. If you want to ensure specific order in which things are done, you either have to move away from using threads, or use explicit synchronization.

So am I right to assume that this weird order of prints is because start() does not guarantee order of execution?

That’s right. When you start a thread, there’s basically a race condition between the main thread and the newly created thread. This means that nothing can be said about the relative order in which things happen between the two threads. If you want to ensure specific ordering, use synchronization.

Leave a Comment