Java Video Tutorial 17

preview_player
Показать описание


In this Java Video Tutorial I cover how to use Java threads.

A thread is just a block of code that is expected to execute while other blocks of code execute. That's it. When you want to execute more than one block of code at a time you have to alert Java.
Рекомендации по теме
Комментарии
Автор

Thanks for the tuts. My favorite part is where you speak fluent English throughout the entire series.

dksharktooth
Автор

I always prefer to implement the interface, but in this tutorial I wanted to show all of the different ways to use threads. I always considered this tutorial to be a video API so that is why I cover everything even if there is a preferred way. I hope that makes sense

derekbanas
Автор

A thread is just a list of code that executes for a period of time and then ends. The main function is a thread. When you implement runnable, that means you promise to make a method named run. When start() is called on that object, the code in run() executes until it finishes. This is all being done so we can have more than one group of code executing at the same time. Kind of like walking and chewing gum :) Feel free to ask more questions if that didn't clear up everything

derekbanas
Автор

Yes, if the thread doesn't have further actions to perform it ends. The thread runs until an exception is thrown, or it reaches the end of the run method. Always feel free to ask questions

derekbanas
Автор

Yes any class that extends Thread must implement the run method. A thread is just a block of code that executes like any other block of code. The code in main is technically a thread

derekbanas
Автор

Thank you very much :) If you stick with it you'll be a great programmer. Just try to have fun. That is the key. I always recommend the Head First Java book because it is fun. If you use it with the numerous java tutorials online you'll have no problems.

derekbanas
Автор

You can only extend one class, but you can implement many interfaces as you'll soon see. I used both extends Thread and Runnable to show you both ways to use threads and for no other reason.

derekbanas
Автор

Yes that is it. Keep at it and it will start to make sense. Don't try to memorize everything and feel free to ask questions

derekbanas
Автор

I cover everything here numerous times through the course of the tutorial. Feel free to ask questions. Soon you'll see that I always try to avoid extending classes. It limits your flexibility

derekbanas
Автор

The main function is in the class LessonSeventeen. It just tells the threads to run and they'll continue to run until they execute all of the functions in the thread. Technically the main function is a thread on its own

derekbanas
Автор

The for loop is used to make sure the date information just prints 20 times and then stops. It is to show that you can use for loops inside of threads more than anything else. I hope that explains it

derekbanas
Автор

It is the correct way to retrieve an instance of DateFormat. You don't have to use a constructor to generate an object. The class can contain a class for example like getTimeInstance that calls the constructor for you and then returns the object

derekbanas
Автор

Sorry about that. We will use threads more in the tutorial. You'll get used to them

derekbanas
Автор

Programming to me has always felt like magic. If you combine programming with electronics knowledge you can pretty much do anything you can imagine :) You're very welcome.

derekbanas
Автор

@TeenAviator It is probably easiest for me to tell you to watch part 15 of this tutorial. In it I completely cover interfaces. Your questions relate to another thing called an abstract class. You use interfaces when you need to extend a class with a super class. You use abstract classes when you need a interface with flexibility. If it doesn't answer your questions tell me :)

derekbanas
Автор

The thing I love about your videos is they aren't intimidating. Even if I don't know something, I'm not scared away by it because you explain it so well. Keep up the great work! Glad to finally get an understanding of threads.

PressStartLetsPlay
Автор

I hope you find them useful. Those topics aren't really even taught in many books and most universities just gloss over the topics. I'm happy that I'm able to fill in the gaps :)

derekbanas
Автор

@sludm Most definitely. I have a more advanced thread tutorial in the works. This was just the basic stuff

derekbanas
Автор

sleep temporarily stops execution of a thread. The InterruptedException exception is thrown when there is a problem shutting down a thread. I almost always use interfaces (using implement) over extending. Yes one reason is because you can implement multiple interfaces. I prefer to not use subclassing if possible

derekbanas
Автор

Hi Derek ! I am a no new programmer but a new one in Java and want to thank you for sharing your knowledge which has helped me a lot. Many, many thanks. I am following your channel now.

RicardoMedinaRojas