what's the state of the process when it is multithread? - multithreading

The state of a process is defined by the current activity of that process.
new
running
waiting
ready
terminated
So if it is multi thread, has more than one state?

Yes. Each thread of execution can be ready to run, running, or blocked. Other possible states exist on some platforms as well.

Related

Daemon And Non-Daemon Threads In Java

JVM in Java is responsible to create a Non-Daemon Thread when executing a Java program. Is it correct?
If so, who is responsible to create Daemon Threads in Java?
Programmers and JVM both create Non-Daemon Threads? Is it correct?
Looking for a clear explanation.
Thanks in Advance.
It doesn’t matter whether “the JVM” or “the programmer” started a thread. A thread is a daemon thread when setDaemon(true) has been called on it before starting or a daemon thread created the thread without calling setDaemon. That’s it.
The documentation of Thread also says:
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class).
There is no responsibility to create daemon threads. Marking a thread as daemon has only one implication which the documentation continues to explain:
The Java Virtual Machine continues to execute threads until either of the following occurs:
The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
So, that’s the only implication; the existence of a non-daemon thread may prevent the JVM from terminating automatically, whereas threads marked as daemon do not.

Windows service onstop will kill the thread?

I have a windows service where I create a thread for doing the background process. So once the thread finishes the task, do I need to call the service ‘stop’ event to make the service stop? (like this.stop() in code)
I didn’t clearly understand why it’s asking to not write code in onstart event and asked to create a thread. Is it because the service will stop after a particular time? Will it cause my thread also to stop, when the service ends? Because I don’t need that. The thread should handle a long running process; I need the thread not killed until it finishes the task.

Difference between looper and a service

I have read that a service is different from thread because it is not affected by the current activity. But threads are. But Looper is also same like a service which doesn't get affected by the current activity and they both run in background.
So what basically differs a service and a looper... ?
Please help me out. I am totally confused in these topics.
Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads do not have any queue. For example, Simple threads do not have any queue. They are for one time execution and after the end of the code the thread will be stopped/killed and it will not able to run another Message(Runnable).
Usage
If you wants to execute multiple messages(Runnables) then you should use the Looper class which is responsible for creating a queue in the thread. For example. If you are writing an application which downloads files from the internet then you can use Looper class to put all the files in the queue to be downloaded.
Service runs in a different process, Say in an application if it gets crashed or the process in which application was running gets killed then Async Task/looper will also be killed but not Service because service was running in a differnt process. Looper run in the same thread as of app unkike Service .Also Android Recommends to use Async task rather than Loopers.

When a process or thread gets blocked, does it wait forever for a notification or just sleep for a while?

I stumbled into this question when I was reading “JVMs typically implement blocking by suspending the blocked thread and rescheduling it later” from http://www.ibm.com/developerworks/java/library/j-jtp04186/?S_TACT=105AGX52&S_CMP=cn-a-j
When we say a process or thread gets blocked when doing IO operations (read, write) or getting access to some exclusive resource (lock, synchronized), when will it get to re-execute? are they constantly waiting until getting a notification from somewhere or does it simply quit its turn and run again after a while?
Has it anything to do with the specified platform? os or jvm?
That would devolve to the underlying OS that must provide threading support to the VM - has to be that way so that the Java app can co-exist harmoniously with all the other proceses and threads that are typically loaded on an OS - browsers, sidebars, anitvirus, video/audio players, Torrent clients, OS internal threads etc. etc.
The code of a blocked thread gets no CPU cycles at all. A thread in that state is just an unused-for-now stack allocation and an extra struct/class pointer in a container in the kernel, waiting for something else to change its state. If it remains blocked or an extended time, the stack may even get swapped out on a busy system.
So yes, they constantly waiting until getting a notification from somewhere.

Scheduling the created thread immediately

i am using PsCreateSystemThread() for creating a thread. But it is not getting scheduled immediately. How can i force that thread schedule immediately after the creation of the thread.
Scheduling can be forced by KeSetPriorityThread and/or KeSetBasePriorityThread.

Resources