Java – Behaviour of Thread sleep in a single core processor machine

javamultithreadingnet

I'm trying to understand how threading works when using a single core, not hyper-threaded processor.

I have two identical console applications running on a machine that has one core CPU and 1 GB of memory.

In this console application, I have a method that starts a new Thread(). I know that when we Start() a new thread, and Join() the same thread, it blocks the main thread that calls it.

My question is, if I have two console applications doing this, will one block the other?

If so, and in the case that I have a Thread.Sleep(10000) on this console app, in this time, if this sleep is running on one console, will the other freeze?

Best Answer

Starting a new thread, by itself, does not cause the current thread to sleep or freeze. The OS schedules these threads (alongside many other threads) to take turn executing.

If the first application starts the second application as a process, via System.Diagnostics.Process, this also does not cause the current process or thread to sleep or freeze. The threads that run in these two applications (processes) will also take turn executing.

In other words, if anything ever sleeps or freezes, it is either due to your code, or is due to "blocking I/O" in which some input/output operations were performed in a way that doesn't resume execution until that operation is completed. Blocking I/O will only block the thread on which it is performed.

If an application is given OS administrative privilege, it is possible to issue some special OS command to forcibly freeze another process or thread. This is an advanced topic and is typically not encountered in typical software development.

To get a view of processes and threads, you can try the "SysInternals Process Explorer" utility. However, I must warn you that if you misuse this utility, (e.g. randomly terminating applications or threads) it can cause abnormal software operations, data corruptions, or even make your computer stop working, requiring a repair.