From the course: Advanced Java Programming
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
The Thread class in Java - Java Tutorial
From the course: Advanced Java Programming
The Thread class in Java
- [Instructor] There are two ways to use threads in Java. One of the ways is to use a class that extends the thread class. In this example, I have a class called ThreadExample, which extends the Thread class. In this class I have also overridden the run method. Any calls that you want the thread to execute goes in this run method. And I could put anything I like in here. In this example I have just put a while loop that prints the numbers one to 100, and after each number I print the name of the current thread. I do this by calling this.getName. I also have a class called Main with a main method in it, which is currently empty. If I was to run this Main class it would actually start a thread. Every time you run any Java program it creates at least one thread. I can demonstrate this by putting a print statement in my Main method, and calling the static activeCount method of the thread class. When I run my application the output shows me that there is already one thread running. I can…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.