Threading in Java

Beka Kodirov
4 min readJun 22, 2016

--

Part I. General questions.

  • What is the difference between Thread and Process?

Processes have been invented in order to provide multitasking in OS level. It is much easier allocating processor time and memory(resources) with processes in OS kernel level. Every application in the OS runs within its process. By default Linux system creates one process a application. But, if you want you can start a particular part of you app within separated process. Process usually has particular information about itself, such as:

  1. PID — Process ID (Unique identifier of process)
  2. PPID — Parent processes id (Id of the process which current process was created from)
  3. UID — User ID
  4. Heap — The address space allocated to this process at the RAM. This address is private for every single process.

Every process by default has a thread(ui or main thread). As soon as all threads died, process is terminated. One process might have a lot of thread but one thread can not belong to more than one process.

Threads can use shared memory(heap) among them. They can use the same object simultaneously, meanwhile, every process has its private heap and can not access to the heap of another process.

All thread in java are native Linux threads, a.k.a pthreads(POSIX). Thread belongs to the process where it was created. (Threads are considered roots of GC)

  • Compare cooperative and preemption multitasking

There are a lot types of multitasking types, such as: non preemption multitasking, cooperative multitasking and preemption multitasking.

Cooperative multitasking —A type of multitasking where process gets whole resources of CPU. Background process can occupy CPU, if only main process done it’s tasks and allows grabbing CPU time to another process. In this kind of multitasking system, process may get as much CPU time as need. Every process pass CPU time to each other sequentially. The disadvantage of cooperative approach is that, if some process hangs on it can not free up CPU usage.

Preemption multitasking — This is more modern approach. In this kind of system it is up to OS providing and managing CPU time to particular process. This kind of multitask system provides faster response for user actions. This multitask based on prioritization of process. For example: lower prioritized process can be eliminated by process the process which has higher priority.

  • What are a “Green Threads” and is there green threads in Java(HotSpot JVM7)?

Java brings new type of threads called “Green thread”. Green thread are imitation of native threads. In this case, JVM cares about whole lifecycle of green thread and switching among them, instead OS kernel. Meanwhile, JVM work only within single native thread. Threads are more expensive in POSIX systems. Moreover, switching native threads is expensive operation rather than green thread.
However, there are drawbacks. The biggest is that you can not execute two threads simultaneously. Since there is only one native thread, only it is called by OS scheduler. Even if you have multiple processors and multiple green threads, only one CPU can utilize the green thread. And all because in terms of the OS Scheduler it all looks like one thread.

  • What is a Thread scheduler? Do you know what kind of algorithm it use?

A system with multiple simultaneously running applications has a scheduler. For the user to perceive that applications can run in parallel, the CPU has to share its processing time among the application threads. The sharing of a CPU’s processing time is handled by a scheduler. That determines what thread the CPU should process and how long it needs to last. The scheduling strategy can be implemented in various ways, but it is mainly based on the thread priority: a high-priority thread gets the CPU allocation before a low-priority thread, which gives more execution time to high-priority threads. Thread priority in Java can be set between 1 (lowest) and 10 (highest), but — unless explicitly set — the normal priority is 5.

  • What are the benefits of multithreading in single core machine?

There was always multithreading when there were single-core processors although, essentially it will be concurrency by time slicing and context switching precisely multitasking. Traditionally single core processors were handled by OS’s for multitasking when you will have a word document, media player and a web browser running simultaneously. Each process will get a small slice of time to perform it’s tasks, meanwhile other processes are paused. However, it is done in milliseconds, therefore an enduser does not have any perception about it. OS used to handle these processes through multitasking until recently when multi-core era arrived now OS handle multiple tasks with more dedicated threads along with time slicing.
If you are dealing with multiple potentially blocking resources — like file IO or GUI interaction or whatnot, then multithreading can be vital.

--

--

Beka Kodirov

Senior Android Software Engineer with deep Java skills. Oracle Certified Professional Java Developer. LinkedIn:https://www.linkedin.com/in/bkodirov/