Threads
Create threads
Thread class
The Thread class
java.lang.Thread;Common constructors of the Thread class
Constructor | Description | Thread() | Creates a default Thread object. | Thread(Runnable) | Creates a Thread object from any object that implements the Runnable interface. | Thread(String) | Creates a Thread object with the specified name. | Thread(Runnable, String) | Creates a Thread object with the specified name from any object that implements the Runnable interface. |
---|
Common methods of the Thread class
Method | Description | run() | Implements the run method of the Runnable interface. This method should be overridden in all subclasses to provide the code that's executed by a thread. | start() | Places a thread in the Runnable state so it can be run by the thread scheduler. | getName() | Returns the name of a thread. | currentThread() | A static method that returns a reference to the currently executing thread. | setDaemon(boolean) | If the boolean value is true, marks a thread as a daemon thread, a subordinate thread that ends when the thread that created it ends. | yield() | A static method that causes the currently executing thread to pause so other threads can run. | sleep(long) | A static method that places the currently executing thread in the Blocked state for the specified number of milliseconds so that other threads can run. | interrupt() | Interrupts a thread. | isInterrupted() | Returns a true value if a thread has been interrupted. | setPriority(int) | Changes a thread's priority to an int value from 1 to 10. |
---|
Constructors and methods of the Thread class
- By the default, the threads that you create explicitly are named numerically. If that's not what you want, you can specify the name on the constructor of the thread.
- By default, a thread is independent of the thread that created it. This is called a user thread. If that's not what you want, you can use the setDaemon method to create a daemon thread.
- The sleep method throws InterruptedException, which is a checked exception.