Performance evaluation on Java single thread and multi-thread has already been discussed in the previous post. So I will show you all the performance of .NET thread comparable to Java thread. We did the same scenario and experimental environment as my last post. The overall Java performs much better than .NET both single thread and multi-thread.
Threading could eventually improve system capacity as multi-core and multi-processor are more common in commodity computer. We can spend less than USD 1000 to get Core-i3, Core-i5 or even Core-i7. So threading technique for enabling-threading tasks could maximize the overall system performance. Be remember that not all tasks could take advantage of multi-threading. And multi-thread is not always good since its overhead and cost in coordinating data. I will discuss this scenario in the later post.
Thread implementation in Java is never easier if you do following my 3 steps strictly. However, there might be a lot of question regarding return value from a thread since
run() method returns void. So I will show you how to get return value from a thread.
Just to review how to implement java thread, the 3 steps are:
Threadclass or Implements
- In main thread, initialize thread class and then call
Assume that you understand the differences between Process and Thread. If not please refer to this article (Process vs. Thread). Concurrency can be achieved both in Process and Thread. Developing concurrency process is costly and hard to manage the process behavior since all processes have their own address space. Thread has been motivated in this sense. Thread can take a lot of advantages by supporting parallelism, low cost in creating and destroying, and easy to manage. Be remember that main thread in a process can create several child threads.
Process and Thread are two unit of executions that are not the same in the sense of executing environment. These two terms often are used confusingly. This article will clear all the doubts.
Process is a compiled program requires CPU to execute its instructions and also requires its own memory space for storing its executing environment. If we peep into the memory of computer, Process can be divided at least into 3 segments:
- Text segment: storing program code
- Heap: storing global data
- Stack: storing local data
The purpose of segmentation in memory is to distinguish between instruction and data. Let’s say 0xA2 in hexadecimal. If it is stored in Text Segment, it is MOV instruction. On the hand, if it is stored in Heap or Stack, it equals 162 in decimal.
Some main feature of Process:
- Has its own memory space
- Has its own unique process ID
- Creating and destroying Process is cost heavily
- Communication between processes is done based on Inter-Process Communication (IPC) or pipe (UNIX-like system)
Thread sometimes is called light weight process. However, it has some unique features making it differences from Process.
Some main feature of Thread:
- Thread exists within a process
- Every process has at least one thread called main thread
- The main thread has an ability to create additional threads
- Thread share process’s resources, including memory, open files, socket. This makes efficient in communication among each other but giving some sorts of potential problematic. This problem will be discussed in Mutual Exclusion.
Somebody might know Moore’s Law and what it had been referred to. In April 1965, co-founder of Intel Gordon Moore expressed the prediction of an advancement in developing transistors leaded to gain capacity and processing power to electronic devices particularly computer. The part of his article can be downloaded here: Moore’s Law
The density of transistors on a chip doubles every 18 months, for the same cost.
His theory is pretty right for several decades. This can be proved in the graph above that shows the trend of a number of transistors in CPU scales linearly for a regular period. So we can imply that today computer has very enormous processing power compares to a very large mainframe developed in 1970s.
One common theme of question may ask is that “if a computer is so powerfully like this, why do we need a Distributed Systems?“