What really goes inside the CPU when threads execute?
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.
java multithreading thread-sleep
add a comment |
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.
java multithreading thread-sleep
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.
– markspace
Nov 24 '18 at 3:08
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49
add a comment |
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.
java multithreading thread-sleep
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.
java multithreading thread-sleep
java multithreading thread-sleep
edited Nov 24 '18 at 3:42
W.Ambrozic
901212
901212
asked Nov 24 '18 at 1:56
MeaMea
73
73
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.
– markspace
Nov 24 '18 at 3:08
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49
add a comment |
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.
– markspace
Nov 24 '18 at 3:08
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.– markspace
Nov 24 '18 at 3:08
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.– markspace
Nov 24 '18 at 3:08
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49
add a comment |
2 Answers
2
active
oldest
votes
The threads are subdivisions of a process and share the same memory space.
The scheduler decides what thread goes to execution. And it is SO dependent.
You can find more info here: https://www.javamex.com/tutorials/threads/how_threads_work.shtml
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
add a comment |
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?
Most modern CPUs can run more than one thread at a time. If the CPU cannot actually run as many threads in parallel as threads that wish to run, the scheduler will make a decision of how long to let each thread run. Generally, it picks a time that is short enough to permit operation to feel smooth but not switch so often that the cost penalty of switching threads significantly impacts performance.
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code.
When a thread goes to sleep, it is no longer ready-to-run.
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?
If a thread is still sleeping, it is not ready-to-run and so it cannot get scheduled. A thread sleeps by arranging itself to be made ready-to-run at some future time and then setting itself not ready-to-run. Only threads that are ready-to-run can be scheduled. An exception might apply if the sleep is for a very short amount of time.
The scheduler arranges to schedule the threads that are ready-to-run, descheduling threads that are no longer ready-to-run and switching if a thread occupies the CPU for too long to give other threads a chance to run. Mechanisms exist to allow threads to wait for things (like disk I/O, network I/O, timeouts, other threads to give them work to do,and so on) by arranging to be made ready-to-run when something happens.
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454550%2fwhat-really-goes-inside-the-cpu-when-threads-execute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The threads are subdivisions of a process and share the same memory space.
The scheduler decides what thread goes to execution. And it is SO dependent.
You can find more info here: https://www.javamex.com/tutorials/threads/how_threads_work.shtml
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
add a comment |
The threads are subdivisions of a process and share the same memory space.
The scheduler decides what thread goes to execution. And it is SO dependent.
You can find more info here: https://www.javamex.com/tutorials/threads/how_threads_work.shtml
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
add a comment |
The threads are subdivisions of a process and share the same memory space.
The scheduler decides what thread goes to execution. And it is SO dependent.
You can find more info here: https://www.javamex.com/tutorials/threads/how_threads_work.shtml
The threads are subdivisions of a process and share the same memory space.
The scheduler decides what thread goes to execution. And it is SO dependent.
You can find more info here: https://www.javamex.com/tutorials/threads/how_threads_work.shtml
answered Nov 24 '18 at 2:04
Guilherme Henrique Santos SousGuilherme Henrique Santos Sous
11
11
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
add a comment |
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
Threads share heap space, but not stack space. The distinction can be (often is) important.
– markspace
Nov 24 '18 at 2:49
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
They share stack space too. They follow an agreement that ensures that they don't use the stack of another thread but the OS doesn't prevent it, because all threads in a process share the same memory space. (In Java, without native code or the Unsafe class, you can't mess up another rhread's stack)
– Erwin Bolwidt
Nov 24 '18 at 3:13
add a comment |
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?
Most modern CPUs can run more than one thread at a time. If the CPU cannot actually run as many threads in parallel as threads that wish to run, the scheduler will make a decision of how long to let each thread run. Generally, it picks a time that is short enough to permit operation to feel smooth but not switch so often that the cost penalty of switching threads significantly impacts performance.
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code.
When a thread goes to sleep, it is no longer ready-to-run.
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?
If a thread is still sleeping, it is not ready-to-run and so it cannot get scheduled. A thread sleeps by arranging itself to be made ready-to-run at some future time and then setting itself not ready-to-run. Only threads that are ready-to-run can be scheduled. An exception might apply if the sleep is for a very short amount of time.
The scheduler arranges to schedule the threads that are ready-to-run, descheduling threads that are no longer ready-to-run and switching if a thread occupies the CPU for too long to give other threads a chance to run. Mechanisms exist to allow threads to wait for things (like disk I/O, network I/O, timeouts, other threads to give them work to do,and so on) by arranging to be made ready-to-run when something happens.
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
add a comment |
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?
Most modern CPUs can run more than one thread at a time. If the CPU cannot actually run as many threads in parallel as threads that wish to run, the scheduler will make a decision of how long to let each thread run. Generally, it picks a time that is short enough to permit operation to feel smooth but not switch so often that the cost penalty of switching threads significantly impacts performance.
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code.
When a thread goes to sleep, it is no longer ready-to-run.
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?
If a thread is still sleeping, it is not ready-to-run and so it cannot get scheduled. A thread sleeps by arranging itself to be made ready-to-run at some future time and then setting itself not ready-to-run. Only threads that are ready-to-run can be scheduled. An exception might apply if the sleep is for a very short amount of time.
The scheduler arranges to schedule the threads that are ready-to-run, descheduling threads that are no longer ready-to-run and switching if a thread occupies the CPU for too long to give other threads a chance to run. Mechanisms exist to allow threads to wait for things (like disk I/O, network I/O, timeouts, other threads to give them work to do,and so on) by arranging to be made ready-to-run when something happens.
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
add a comment |
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?
Most modern CPUs can run more than one thread at a time. If the CPU cannot actually run as many threads in parallel as threads that wish to run, the scheduler will make a decision of how long to let each thread run. Generally, it picks a time that is short enough to permit operation to feel smooth but not switch so often that the cost penalty of switching threads significantly impacts performance.
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code.
When a thread goes to sleep, it is no longer ready-to-run.
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?
If a thread is still sleeping, it is not ready-to-run and so it cannot get scheduled. A thread sleeps by arranging itself to be made ready-to-run at some future time and then setting itself not ready-to-run. Only threads that are ready-to-run can be scheduled. An exception might apply if the sleep is for a very short amount of time.
The scheduler arranges to schedule the threads that are ready-to-run, descheduling threads that are no longer ready-to-run and switching if a thread occupies the CPU for too long to give other threads a chance to run. Mechanisms exist to allow threads to wait for things (like disk I/O, network I/O, timeouts, other threads to give them work to do,and so on) by arranging to be made ready-to-run when something happens.
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?
Most modern CPUs can run more than one thread at a time. If the CPU cannot actually run as many threads in parallel as threads that wish to run, the scheduler will make a decision of how long to let each thread run. Generally, it picks a time that is short enough to permit operation to feel smooth but not switch so often that the cost penalty of switching threads significantly impacts performance.
Also, lets say t1 is scheduled first means it gets CPU time then it goes to sleep for 1000ms as in the above code.
When a thread goes to sleep, it is no longer ready-to-run.
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?
If a thread is still sleeping, it is not ready-to-run and so it cannot get scheduled. A thread sleeps by arranging itself to be made ready-to-run at some future time and then setting itself not ready-to-run. Only threads that are ready-to-run can be scheduled. An exception might apply if the sleep is for a very short amount of time.
The scheduler arranges to schedule the threads that are ready-to-run, descheduling threads that are no longer ready-to-run and switching if a thread occupies the CPU for too long to give other threads a chance to run. Mechanisms exist to allow threads to wait for things (like disk I/O, network I/O, timeouts, other threads to give them work to do,and so on) by arranging to be made ready-to-run when something happens.
answered Nov 24 '18 at 3:26
David SchwartzDavid Schwartz
137k14144226
137k14144226
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
add a comment |
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
So in my example, if t1 is still sleeping then it can't get scheduled, meaning CPU time is given to t3 again unless it isn't still sleeping?
– Mea
Nov 24 '18 at 6:53
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
@Mea Correct. A thread that's not ready-to-run cannot be scheduled until something makes it ready-to-run.
– David Schwartz
Nov 24 '18 at 18:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454550%2fwhat-really-goes-inside-the-cpu-when-threads-execute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you in school? This is part of a "system specialty" in the com sci major. Basically you learn about computers as a "whole system," how the operating system works and how it time slices and stuff. And also common algorithms used by OSs for doing its job, including time slicing programs/threads.
– markspace
Nov 24 '18 at 2:51
Example: cs.stanford.edu/academics/current-masters/… Even if you don't want to do those things, understanding how they work can let you do your job better and easier, because everybody has to deal with systems even if you don't work on them directly. Personally I'd recommend taking some classes in systems even if you want to specialize in something else.
– markspace
Nov 24 '18 at 2:53
What amount of time a thread has before CPU is given to other threads?
You can look for information on specific subjects just by doing a web search. For example, "linux timeslice": stackoverflow.com/questions/16401294/… It looks like it's 100 milliseconds.– markspace
Nov 24 '18 at 3:08
A web search for "unix internals" or "linux internals" will also get you books and papers on how an OS is put together.
– markspace
Nov 24 '18 at 3:13
@markspace I am currently learning java in school and just got introduced to multi-threading. These were just the questions that I was curious about.
– Mea
Nov 24 '18 at 6:49