From the following code , the ds.g() is getting called first after that the thread named ds.f() is being started but according to the code, the thread should get called first.
Not true. Once the new thread is started, it is now running independent of the main thread. So, whether the f() method or the g() method gets called first depends on the processor, the OS, priority, etc. These two threads are now racing each other, and on some OSes, f() will be called first, on others, g() will execute first.