CodeGym /Courses /Java Core /Multithreading

Multithreading

Java Core
Level 6 , Lesson 1
Available

"Hello, Amigo! We have a new and very difficult topic. I'm sorry. It is often considered one of the most complex topics not only in Java, but also in programming in general. I'm talking about multithreading."

Imagine a typical computer game, for example, a spaceship racing game. You're flying through the expanses of the cosmos, dodging meteorites and patrol cruisers. Two dozen others are participating with you in these illegal races.

Let's say you decide to write such a game. Your program will have to keep track of commands (keyboard input), move the spaceships, calculate their trajectories, determine the consequences of any collisions, and draw all this on the user's screen. This is very complex work.

Remember how we solved «problem of great complexity» in the example about the growing shipping company?

We divided it into independent departments and rigidly specified (standardized) how they could interact.

"But what do we do when the independent parts have to perform some work in parallel with the other parts?! The answer to this question is threads."

Try to imagine a program as a small robot that runs around the code and executes commands. First, it executes a command on one line, then moves to the next, and so on.

"I can see it in my mind. Piece of cake!"

"Very good. And now imagine that you have several of these robots. While one is handling user input, a second is updating objects based on that input. A third executes the code to displaying these objects on the screen. Several times a second, a fourth checks whether any ships have collided and, if they have, calculates the results of the collision."

Thus, we can not only divide the program into independent parts/objects, but also make it so these parts can perform their work independently of each other. The less interaction between the individual parts, the less complex the program.

Imagine that you were able to replace the manager with a script that sends letters. And the other company departments weren't even able to tell there had been a change. This sort of thing happened as early as the 26th century with excellent results. Most managers, and even top executives, can be successfully replaced by a script of average complexity. Only after the «office plankton union» intervened did the mass layoffs of managers come to an end. But I digress.

"How interesting!"

"Not only can there be several of these " little robots" executing code, they can also communicate with each other and spawn new robots."

"Spawn new robots?"

"Yes, to perform new tasks. Sometimes it's advantageous to create another robot (another thread) to perform some action at the same time as the current thread (robot)."

"This sounds like a good thing, but I can't think of where I would use it."

And why do we call them «threads»?

"Imagine that each robot is a different color, and marks commands with its color as it performs them. The path taken by the small robot is like the line left behind by a pencil. This path strings along behind the robot, like a thread behind a needle."

Each «small robot» has a task that it was created to perform. You can think of a thread is the set of commands executed while performing this task.

Let's say you're flying on a spaceship to deliver cargo. Then «deliver cargo» is your task, and you're in the middle of performing it. And your flight path is your thread. We could say that each new task, each task not yet completed, has its own thread (a path that still needs to be traversed).

"In other words, there is a task and a "little robot" that executes it. And a thread is just the path taken by the robot while it completes its task?"

"Exactly."

That's how it all works deep inside. Because the computer has only one processor, it can only execute one command at a time. So here's what happens: the processor constantly switches between threads. It switches to a new thread, executes a few commands, then switches to the next thread, executes a few commands, and so on. But since switching between threads occurs hundreds of times per second, it seems to us that all the threads are running simultaneously.

Multithreading - 1
Comments (24)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Олег Байбула Level 32, Ukraine Expert
23 January 2023
This dialogue fashion is really irritating.
Anonymous #10782038 Level 24, United States of America
16 April 2024
it really is
matemate123 Level 50, Kraków, Poland
8 January 2023
It’s helpful read that all level one more time when you’re almost finish it. It’s more clear now for me, because Java is my first language and for me it’s new idea that Threads. Slowly I start understanding this topic, but still in confusion, and I’m not only with that feeling I suppose. Heads up, and some YouTube materials are helpful too.
Hoist Level 4, San Diego, United States
16 October 2023
Dump the code into Chat GPT and ask #AI to explain it in great detail what is happening line by line in the code and to debug it for you. Also ask it for the best supplemental materials it can locate .... Successful for me to better understand and lay out some psydo code structure to solve
Daniel Whyte Level 17
8 April 2021
"Most managers, and even top executives, can be successfully replaced by a script of average complexity. Only after the «office plankton union» intervened did the mass layoffs of managers come to an end. But I digress" Fantastic lol
Karas Level 1, Tampa, United States
1 March 2021
26th Century?
Pawel Level 22, Austin, TX, USA
3 February 2021
Why in the example run method is performed if nobody call it? And why it is called child thread (marked as red)
Pawel Level 22, Austin, TX, USA
3 February 2021
Never mind, if you have same question just read next lesson. Run method is not random but it comes from runnable interface which is implemented
Zourkas Level 16, Frankfurt, Germany
31 March 2021
The implementation of the start() method contains run() method. run() is being called under the hood of start().
Lawson Level 29, Lagos, Nigeria
30 August 2020
I think this means that thread are set of commands to be executed upon completion of a task... Correct me if i am wrong
Andrei Level 41
11 December 2020
I don't think so. I think that thread is the "In other words, there is a task and a "little robot" that executes it. And a thread is just the path taken by the robot while it completes its task?"
Agent Smith Level 38
29 August 2020
Java’s multithreading features work in both single and multi processor systems. In a singlecore system, concurrently executing threads share the CPU, with each thread receiving a slice of CPU time. Therefore, two or more threads do not actually run at the same time, but idle CPU time is utilized. However, in multicore systems, it is possible for two or more threads to actually execute simultaneously. In many cases, this can further improve program efficiency and increase the speed of certain operations.
Andrei Level 41
11 December 2020
So the picture above, with the print, is how 2 threads would work in a single core system?
Sela Level 20, Poland
23 July 2020
so other threads can run after main thread finishes...
null Level 26, Orlando, United States
12 May 2020
This is actually an Operating System concept, check out good free book here: http://pages.cs.wisc.edu/~remzi/OSTEP/
Hoist Level 4, San Diego, United States
13 November 2022
Good link THX ! Easier to understand programming after learning some assembly / ISA
Manish Sinha Level 1, london, United Kingdom
14 April 2020
new topic ..new things to explore yeah....