software threads vs hardware threads

Software threads are threads of execution managed by the operating system. Hardware threads are a feature of some processors that allow better utilisation of the processor under some circumstances. They may be exposed to/by the operating system as appearing to be additional cores (“hyperthreading”). In Java, the threads you create maintain the software thread abstraction, … Read more

Difference between user-level and kernel-supported threads?

Edit: The question was a little confusing, so I’m answering it two different ways. OS-level threads vs Green Threads For clarity, I usually say “OS-level threads” or “native threads” instead of “Kernel-level threads” (which I confused with “kernel threads” in my original answer below.) OS-level threads are created and managed by the OS. Most languages … Read more

Can I get Unix’s pthread.h to compile in Windows?

pthread.h is a header for the Unix/Linux (POSIX) API for threads. A POSIX layer such as Cygwin would probably compile an app with #include <pthreads.h>. The native Windows threading API is exposed via #include <windows.h> and it works slightly differently to Linux’s threading. Still, there’s a replacement “glue” library maintained at http://sourceware.org/pthreads-win32/ ; note that it has some slight incompatibilities with … Read more

Difference between volatile and synchronized in Java

It’s important to understand that there are two aspects to thread safety. execution control, and memory visibility The first has to do with controlling when code executes (including the order in which instructions are executed) and whether it can execute concurrently, and the second to do with when the effects in memory of what has been done … Read more

When should you use multithreading? And would multi threading be beneficial if the different threads execute mutually independent tasks?

Q: When should you use multithreading? A: “Your question is very broad. There are few non-trivial systems where the functionality can be met simply, quickly and reliably with only one thread. For example: [pick out a typical system that the target company sells and pick out a couple aspects of its function that would be … Read more

Service vs IntentService in the Android platform

Tejas Lagvankar wrote a nice post about this subject. Below are some key differences between Service and IntentService. When to use? The Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must use threads within Service. The IntentService can be used in long tasks usually with no communication … Read more

java.lang.IllegalThreadStateException

You are storing the thread in a field. If the method is called in two threads, the readThread.start() can be called twice for the same thread. You need to ensure readCommand is not called multiple times and perhaps not start the readThread again if its already running. e.g. you can synchronized the method and check … Read more