Start learning now
You've reached Level 16!
Congratulations, you've completed the fifth level of the second quest! You've learned a lot about overloading methods, the order of constructor calls, and variable initialization, and you've completed 34 tasks! On the sixth level, you'll plunge into a super important topic: threads.
Imagine the game "Space Race": you fly, dodging meteorites and patrol cruisers, surrounded by similar starships. The application must track the control commands, move the starships, calculate their trajectories and the consequences of any collision, and draw it all on the screen... How is this done? Answer: multithreading.
Creating and starting new threads
If you've ever wondered how to start and use a new thread, you'll find out quite soon—in this very lesson. And your teacher Rishi Gatesman will help you with this. But if it's never crossed your mind, Rishi Gatesman will explain it anyway, because now is the time.
Practice with threads | Level 6
Your teacher Diego has been waiting for you! He can't wait to give you a bunch of tasks on the difficult topic of threads. In this lesson, we'll reinforce our knowledge through practice, and we'll try not to be confused by threads.
join — waiting for a thread to end
Suppose the main thread has created a child thread to perform some task. Then at some point, the main thread needs the results of work performed by a child thread. But the child thread hasn't finished its work yet. What should the main thread do? This problem is solved by the join method. This lesson will teach you how.
Tasks about join
Your teacher Diego found out that you learned about the join method in the previous lesson. But he's a suspicious and distrustful robot. He's not one to just take somebody's word (as always!), so he's bombarded you with tasks. Well, what's it to you? It's not the first time. Get cracking!
Even programs need to sleep sometimes. And threads need it even more. To put a thread to sleep, Java has a method called sleep. It's declared as a static method of the Thread class. This means that it isn't associated with any specific object. The purpose of this method is to make the program "fall asleep" for a while. How does this work? You'll learn in the lesson!
Today we'll learn to make time stand still in Java programs. We'll do this using the sleep method, the local time keeper. Your teacher Diego has taken special pains to prepare some tasks for you: you'll play around with a stopwatch, create a countdown, and teach a program to find out what time it is in London.
Stopping threads: the unofficial version
Let's say the user tells the program to "download a file from the Internet. The main thread creates a separate child thread for this job, and passes it an object whose run method contains all the necessary actions to download the file. And then — wham — the user changes his mind about downloading the file! How do we cancel the job and stop the thread?
Stopping threads: the official version
This lesson will include many aggressive words like "interrupt". All because we're still studying the possibilities and peculiarities of stopping threads. We'll learn how to use the variable isInterrupt and what the isInterrupted() and interrupt() methods do.
Tasks about interrupt
If you've always dreamed of writing a game, then you should be very interested in threads and time manipulations. Become a master of threads! To make this happen faster, Diego has prepared five interesting tasks to help you learn about the interrupt() method.
Useful links from the Professor — 16
Of course, looking at the same material from different angles is useful, because everyone thinks differently, and perhaps for you the best source of the theory will be different from the source best suited to your friend. Especially for you, the Professor found a very interesting lesson online that will teach you a lot more about threads.