Java Multithreading: Starting Threads (Video Tutorial Part 1)

Does multithreading seem like a black art to you? This is the first part of an advanced Java tutorial on multithreading that hopefully will help you out. In this tutorial we look at the two ways of starting a thread in Java.

This is an advanced tutorial, and as such I assume that you’re OK with extending classes, implementing interfaces and so on.

After starting the video, click the maximise button to make it fullscreen so you can see the code!

Code Examples For This Tutorial

Starting threads by extending the Thread class:

class Runner extends Thread {

	@Override
	public void run() {
		for(int i=0; i<5; i++) {
			System.out.println("Hello: " + i);
			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}


public class Application {

	
	public static void main(String[] args) {
		Runner runner1 = new Runner();
		runner1.start();
		
		Runner runner2 = new Runner();
		runner2.start();

	}

}

Starting threads using the Thread class directly:

class Runner implements Runnable {

	@Override
	public void run() {
		for(int i=0; i<5; i++) {
			System.out.println("Hello: " + i);
			
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
	
}


public class Application {

	
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runner());
		thread1.start();
	}

}

Starting threads using the Thread constructor with anonymous classes:


public class Application {

	
	public static void main(String[] args) {
		Thread thread1 = new Thread(new Runnable() {

			@Override
			public void run() {
				for(int i=0; i<5; i++) {
					System.out.println("Hello: " + i);
					
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
			
		});
		
		thread1.start();
	}

}

Learn to program Java Swing with my complete video course - desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

9 Responses to Java Multithreading: Starting Threads (Video Tutorial Part 1)

  1. Shank says:

    Thanks for your very good examples.

    Please answer the below question

    Question: When and why do we use multi-threading in the application world which has to explain multi threading is required itself.

    Appreciate if you could answer some more application which needs multi threading itself which says we have to use multi-threading programming nothing else.

    • Squiffy says:

      It’s used whenever you need to do two or more things at the same time. For example, you can run tasks like file loading or processing data while at the same time running a GUI. Or a game could have a thread that handles playing sounds, while another thread allows the user to keep playing meanwhile. A server might create a new thread whenever some other computer requests information, so that multiple requests (e.g. for HTML documents) can be handled at the same time. Or if you want to ping 10000 different machines to check if they’re still working (which I had to do in my last full-time job), you don’t want to ping one then wait for it and ping the next and so on. You want to ping lots all at once; so you use multiple threads.

      You really couldn’t write a server application without using multiple threads. If you had only one thread, whenever someone requested a web page from the server, everyone else would have to wait till it had finished sending the page before they could look at another web page.

  2. Vishal says:

    Hi
    Good video it will help to understand way of creating thread.
    As I know there are two way of creating thread but why?
    As per java standard If we extends some class we can not extends another class as not allowing multple class interitance. So we can use runnable is that only diffrence?

  3. Joe says:

    Nice helpfull video on threads.

  4. hugo says:

    (After watching you great tutorials ) I failed my exam because I declared my thread constructor in my “Main” Section , he told me i cant have a method inside another method much less my main method…. is he right ? what do you suggest i tell him?

    • Squiffy says:

      He’s right … unless it’s a method of an anonymous class. You can’t declare methods inside methods. It’s important to create Java programs yourself, not just watch the videos. I should say this more, really. Sorry to hear about your exam.

  5. Anand says:

    Hi All,
    I have placed few java multi threading example using Thread & Runnable interface. Also creating ThreadPool class using ThreadGroup has give in this blog – http://www.javadiscover.blogspot.com/search/label/Threads

    Thanks
    Anand

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Multithreading, Java Video Tutorials (Advanced) | Tagged , , , , |