Start learning now
You've reached Level 17!
Congratulations, you've completed the sixth level of the quest! You've touched on an important topic (threads) and completed 34 tasks (or perhaps less if you left some for later). On the seventh level, we'll continue to study threads. In the near future, we'll deal with the static method yield and the keywords synchronized and volatile.
Multithreading problems: accessing a shared resource
Threads are, of course, cool and open a world of incredible opportunities (and a heap of complexities) to programmers. Different threads change the state of different objects based on the tasks they perform. And, of course, they can step on each other's toes! But the most frightening part happens deep within the Java machine... Does that sound intriguing?
Tasks involving a shared resource: console output
A wise teacher once said that a program is a set of objects that "little robots" (threads) crawl through, executing commands contained in the methods. But the big little robot Diego can't deprive you of the pleasure of doing some tasks involving a shared resource. There are only two, but you're going to have to do some thinking.
Threads aren't independent! Synchronized!
Uncontrolled thread switching is another problem. We face a reasonable question: why not let threads do what they want and control their own switching? In fact, this approach creates even more problems. This lesson will teach you about these problems and what can be done.
Synchronized! The practical version
Your teacher Diego can't hide anything. He's learned that in the previous lesson you got acquainted with blocking threads, and learned what mutexes are and how to work with them, and what the synchronized keyword is for. Five tasks about synchronized are waiting for you, courtesy of Master Diego!
The local cache: a multithreading problem. Volatile
Houston, we have a problem with multithreading! Yes, again. This time it's related to how cache memory is used. The very cache where the processor places its most frequently used variables. It does this to accelerate its work, but herein lies the root of evil. Quite soon, you'll learn what the catch is and how to solve this problem.
Practice with volatile
The volatile modifier causes a variable to always be read from and written to normal memory shared by all threads. And thanks to Diego, you won't have to wait to start doing two tasks about volatile. You'll be one small step closer to the title of junior Java developer.
yield (surrendering the right of way)
You already know that the processor constantly switches between threads. Each thread is given a quantum — a piece of processor time. When the time is up, the processor switches to another thread and executes its commands. And if you need to end the current thread's quantum prematurely, the Thread class's static method yield comes to your aid!
Useful links from the Professor — 17
Especially for you, Professor Noodles found a very interesting lesson online that will teach you a lot more about synchronized, volatile, and yield. He recommends that you study it, but don't go too deep: true immersion in this topic will come a little later.
What’s inside your computer?
We need Java and other languages to facilitate communication between people and computers. Handling even simple actions utilizes critical computer components such as peripherals, the input/output subsystem, CPU, and memory. This video explains the steps your computer takes every time you click with the mouse.
Bonus task | Lesson 10 | Level 7
Captain Squirrels is on the phone! He has a couple of surprises for you. More precisely, not a couple, and they aren't surprises, but rather ten tasks to reinforce your knowledge about threads. And the truly curious can try their hand on three bonus tasks that are more difficult. For example, in one of them you'll deal with an unlucky waiter in a restaurant.