Starting a Thread

public class MyThread extends Thread {
											public void run() {
											// do some work
											}
											}
											// code to use MyThread
											new MyThread().start();

There are two primary techniques for writing code that will run in a separate thread. You can either implement the java.lang.Runnable interface or extend the java.lang.Thread class. With either approach, you must implement a run() method. The run() method contains the code that you want to execute in the thread. In this phrase, we have extended the java.lang.Thread class. At the point where we want to start the thread, we instantiate our MyThread class and call the start() method, which is inherited from the Thread class.

Here, we show how running a thread is accomplished using the other technique of ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.