Java - Thread Java Threads

Creating a Thread

Java lets you represent a thread as an object.

An object of the java.lang.Thread class represents a thread.

There are two steps involved in working with a thread:

  • Creating an object of the Thread class
  • Invoking the start() method to start the thread

You can use the default constructor of the Thread class to create a Thread object.

// Creates a thread object
Thread simplestThread = new Thread();
// Starts the thread
simplestThread.start();

The start() method puts the thread in the runnable state.

In this state, the thread is ready to receive the CPU time.

Invoking the start() method just schedules the thread to receive the CPU time.

The following code gets you started using threads.

public class Main {
        public static void main(String[] args) {
                // Creates a thread object
                Thread simplestThread = new Thread();

                // Starts the thread
                simplestThread.start();
        }
}

Related Topics