Introduction - Java Multithreading
Introduction - Java Multithreading
Ask a Question
Introduction
This lesson details the reasons why threads exist and what benefit do they provide. We also discuss
the problems that come with threads.
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 1/6
22/04/2023, 09:53 Introduction - Java Multithreading for Senior Engineering Interviews
Threads like most computer science concepts aren't physical objects. The closest tangible manifestation of
threads can be seen in a debugger. The screen-shot below, shows the threads of our program suspended in the
debugger.
The simplest example to think of a concurrent system is a single-processor machine running your favorite
IDE. Say you edit one of your code files and click save, that clicking of the button will initiate a workflow
which will cause bytes to be written out to the underlying physical disk. However, IO is an expensive
operation, and the CPU will be idle while bytes are being written out to the disk.
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 2/6
22/04/2023, 09:53 Introduction - Java Multithreading for Senior Engineering Interviews
Whilst IO takes place, the idle CPU could work on something useful and here is where threads come in - the IO
thread is switched out and the UI thread gets scheduled on the CPU so that if you click elsewhere on the
screen, your IDE is still responsive and does not appear hung or frozen.
Threads can give the illusion of multitasking even though at any given point in time the CPU is executing only
one thread. Each thread gets a slice of time on the CPU and then gets switched out either because it initiates a
task which requires waiting and not utilizing the CPU or it completes its time slot on the CPU. There are much
more nuances and intricacies on how thread scheduling works but what we just described, forms the basis of
it.
With advances in hardware technology, it is now common to have multi-core machines. Applications can take
advantage of these architectures and have a dedicated CPU run each thread.
Benefits of Threads
1. Higher throughput, though in some pathetic scenarios it is possible to have the overhead of context
switching among threads steal away any throughput gains and result in worse performance than a
single-threaded scenario. However such cases are unlikely and an exception, rather than the norm.
All other benefits of multi-threading are extensions of or indirect benefits of the above.
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 3/6
22/04/2023, 09:53 Introduction - Java Multithreading for Senior Engineering Interviews
42
43 t1.join();
44 t2.join();
45
46 long finalCount = s1.counter + s2.counter;
47 long end = System.currentTimeMillis();
48 System.out.println("Two threads final count = " + finalCount + " took " + (end - start));
49 }
50
51 static public void oneThread() {
52
53 long start = System.currentTimeMillis();
54 SumUpExample s = new SumUpExample(1, MAX_NUM );
55 s.add();
56 long end = System.currentTimeMillis();
57 System.out.println("Single thread final count = " + s.counter + " took " + (end - start));
58 }
59
60
61 public static void runTest() throws InterruptedException {
62
63 oneThread();
64 twoThreads();
65
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 4/6
22/04/2023, 09:53 Introduction - Java Multithreading for Senior Engineering Interviews
66 }
67 }
68
69
In my run, I see the two threads scenario run within 652 milliseconds whereas the single thread scenario
runs in 886 milliseconds. You may observe different numbers but the time taken by two threads would
always be less than the time taken by a single thread. The performance gains can be many folds depending on
the availability of multiple CPUs and the nature of the problem being solved. However, there will always be
problems that don't yield well to a multi-threaded approach and may very well be solved efficiently using a
single thread.
1. Usually very hard to find bugs, some that may only rear head in production environments
2. Higher cost of code maintenance since the code inherently becomes harder to reason about
3. Increased utilization of system resources. Creation of each thread consumes additional memory, CPU
cycles for book-keeping and waste of time in context switches.
4. Programs may experience slowdown as coordination amongst threads comes at a price. Acquiring and
releasing locks adds to program execution time. Threads fighting over acquiring locks cause lock
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 5/6
22/04/2023, 09:53 Introduction - Java Multithreading for Senior Engineering Interviews
contention.
With this backdrop lets delve into more details of concurrent programming about which you are likely to be
quizzed in an interview.
Next
Mark as Completed
https://www.educative.io/courses/java-multithreading-for-senior-engineering-interviews/m2ygV4E81AR 6/6