I think I am overthinking the concept of threads in Java, and this might be a dumb question. I read some of the answered topics on threads, but they don't actually answer my question.
So suppose we have 3 threads: t1, t2, t3.
t1's run method:
thread.sleep(1000);
System.out.println("Hello");
t2's run method:
System.out.println("Hi");
t3's run method:
thread.sleep(3000);
System.out.println("Bye");
t1.start();
t2.start();
t3.start();
I understand that any of these threads could be scheduled first. But what actually goes inside the CPU? I've read in a book that each of these thread gets some CPU time and threads are switched inorder to create a illusion of parallel running. What amount of time a thread has before CPU is given to other threads?
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code. Now lets say t2 gets CPU time, and since it isn't sleeping, it prints 'Hi'. Now t3 gets cpu time, it sleeps for 3000ms therefore t1 gets the cpu time again. So now does t1 picks up from where it has left before? what if t1 is still sleeping because 1000ms hasn't passed since the last time?
I might be taking it in a wrong way, but I'm juggling through these concepts.