Threading in Java

Every thread in Java needs a Runnable. To make a runnable, you must first create a class which implements the Runnable interface. Runnable interface has a method called run() which gets called by the Thread. Your class will need to have this method. Put your code in this run() method. Your Runnable class should look similar to:

Now, you can create a thread in multiple ways but the most appropriate way is to pass the runnable object in its constructor.

Make sure that you say thread1.start() and not thread1.run(). thread1.run() simply calls the run method within the runnable while thread1.start() runs the run method in separate thread.

That’s it! Now you know the basics of threading in Java!

Check out the video tutorial here:

7 thoughts on “Threading in Java

  1. Hey this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you
    have to manually code with HTML. I’m starting a blog soon but have no coding skills so
    I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

    1. Hey Regan,

      If this is your first time, you might want to try out something like wordpress.com or blogger.com. They both provide WYSIWYG editors and support for loads of themes to style your blog. Check it out.

      Manthan

  2. I don’t even understand how I finished up here, however I thought this submit was
    good. I don’t understand who you might be but definitely you
    are going to a well-known blogger in case you are not already.

    Cheers!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.