Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Java Dictionary

  1. Home
  2. Java Dictionary
  3. new Thread() / thread.start() / Runnable

new Thread() / thread.start() / Runnable

Threads let you run multiple tasks concurrently. Create a thread by extending the Thread class or implementing the Runnable interface, then call start() to launch it.

Syntax

// Recommended: pass a Runnable as a lambda expression.
Thread t = new Thread(() -> {
    // Code to run in the thread
});
t.start();

// Implement the Runnable interface.
class MyTask implements Runnable {
    @Override
    public void run() {
        // Code to run in the thread
    }
}
Thread t = new Thread(new MyTask());
t.start();

// Extend the Thread class.
class MyThread extends Thread {
    @Override
    public void run() {
        // Code to run in the thread
    }
}
new MyThread().start();

// Pauses the current thread for the specified number of milliseconds (watch out for InterruptedException).
Thread.sleep(1000);

// Waits for the thread to finish.
t.join();

Main Methods

MethodDescription
start()Starts the thread. Internally, run() is executed on the new thread.
run()Contains the code to execute in the thread. Calling it directly runs it on the current thread instead.
Thread.sleep(long millis)Pauses the current thread for the specified number of milliseconds. Requires handling the checked exception InterruptedException.
join()Causes the calling thread to wait until this thread finishes.
Thread.currentThread()Returns the currently executing thread.
getName() / setName()Gets or sets the thread's name. Useful for debugging.
isAlive()Returns whether the thread is still running.
interrupt()Requests an interrupt on the thread. If the thread is waiting in sleep() or similar, an InterruptedException is thrown.

Sample Code

// Create and start a thread using a lambda expression.
Thread t1 = new Thread(() -> {
    for (int i = 1; i <= 3; i++) {
        System.out.println("Thread A: " + i);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}, "Thread A");

Thread t2 = new Thread(() -> {
    for (int i = 1; i <= 3; i++) {
        System.out.println("Thread B: " + i);
    }
}, "Thread B");

t1.start();
t2.start();

// Use join() to wait for both threads to finish before continuing.
try {
    t1.join();
    t2.join();
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}
System.out.println("Both threads have finished.");

// Implement Runnable as a class.
class Counter implements Runnable {
    private int count;

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) count++;
        System.out.println("Count: " + count);
    }
}
new Thread(new Counter()).start();

Notes

Threads let you run time-consuming I/O or CPU-intensive tasks concurrently, improving application responsiveness. When multiple threads read or write the same data, race conditions can occur. Use the synchronized keyword or classes from the java.util.concurrent package to ensure proper synchronization.

In real applications, it is recommended to use ExecutorService to manage a thread pool rather than creating Thread instances directly.

If you find any errors or copyright issues, please .