1

My question is regarding threads in general(ex: in Java).

The problem : when a thread is being in (Runnable - state) - i.e: it is executing, and it is giving an instruction (say by invoking the method addOneToX(int x)), is it possible for the thread to quit or stop its work before finishing the instruction but after it has started executing it. in other words, most of instructions in high-level languages are decoded into the machine-specific language and decomposed to a number of machine-cycles (clock-cycles) in the CPU. So I guess it is clear, accordingly:

1> What is the minimum time given to a thread to be in the Runnable state?

2> How does the thread save its state so that it comes to it later? (i.e: when it quits Runnable state and comes back to it later to continue from where it stopped)

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Profess Physics
  • 317
  • 4
  • 11
  • 1) There is no minimum. 2) Way too complicated to cover here. Does it matter how the OS saves and restores the registers of the CPU, when your language of choice (Java) is so very far removed from machine instructions? – Andreas Mar 21 '17 at 07:57
  • 3
    You'll need to read a good book about operating systems, scheduling, time slicing, context switching and the like. For StackOverflow, this is too broad. – RealSkeptic Mar 21 '17 at 07:58
  • Related: http://stackoverflow.com/questions/16401294/how-to-know-linux-scheduler-time-slice – Jiri Tousek Mar 21 '17 at 08:10
  • Thx for replying. but the logic in my head (which could be wrong) says that the absolute minimum is one machine-cycle since the internal clock of a CPU produces these (say pulses) at a fixed rate for the sake of (let me call it - low level synchronization) where every pulse is an absolute internal operation. As for understanding this., I find it important for the sake of (let me call it - high-level synchronization) ., by which I mean synchronizing (say in Java). – Profess Physics Mar 21 '17 at 08:11
  • See, the whole problem (when it comes to the high-level synchronization) is that case whenever 2 threads are accessing a method.., if say there is nothing guaranteed then I have to use synchronization which is costly almost every time I am in such a case. So I wanted to narrow the possibility for such a trap down by low-level understanding.., if there is however some frame(instructions) that are sufficient to solve this problem.., then I guess it is okay ^^. – Profess Physics Mar 21 '17 at 08:16

2 Answers2

2

There is no guaranteed minimum time.

The scheduler decides what the time slice will be. Usually you could expect anything from fractions of a millisecond to about 100ms. But often this value will by dynamic. Also, a thread might even encounter an extreme like running only one instruction which happens to be I/O, upon which the thread blocks and is pushed out of CPU.

The high-level language instructions are eventually translated into (possibly) multiple CPU instructions. The CPU instruction is the atomic part that will be executed without interruption, other than that the program may be interrupted at any place between two instructions, even in the middle of a high-level language command. Note that there are some specific CPU instructions (like atomic get-and-set or get-and-increase) that can be used for thread synchronization.

The basic (much simplified) idea of storing the thread's state is: store the registers in RAM and store the pointer to current instruction.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
1

What is the minimum time given to a thread to be in the Runnable state?

Most practical Java implementations use native threads: That is, they allow the operating system to take care of the details of scheduling threads. Most modern operating systems offer a choice of various thread-scheduling algorithms, and most algorithms offer a number of configurable parameters. There is no single answer to your question.

Almost certainly, it will be less than one second. Quite likely less than 100 milliseconds. Other than that, it's hard to say.

How does the thread save its state so that it comes to it later?

The state of a thread (in most programming languages, Java included) consists of its call stack, and it's CPU registers. The CPU registers include one that points to the top of the call stack, one that points to the current instruction, and usually others.

When it's time to switch threads, the OS interrupts the processor (an interrupt basically forces an immediate function call) and the interrupt handler routine saves all of the CPU registers to a memory location that is reserved for the current thread. Then it restores the registers for some other thread and basically "returns" to wherever that other thread was interrupted.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57