0% found this document useful (0 votes)
6 views6 pages

Creating A Thread

The document explains three methods to create a thread in Java: extending the Thread class, implementing the Runnable interface, and using the Callable interface with ExecutorService. It provides code examples for each method, demonstrating how to execute threads and print their names. Additionally, it shows how to implement Runnable using a lambda expression for a more concise syntax.

Uploaded by

Jack
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Creating A Thread

The document explains three methods to create a thread in Java: extending the Thread class, implementing the Runnable interface, and using the Callable interface with ExecutorService. It provides code examples for each method, demonstrating how to execute threads and print their names. Additionally, it shows how to implement Runnable using a lambda expression for a more concise syntax.

Uploaded by

Jack
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Concurrency

Creating a Thread
How to create a thread?
• there are three ways to create a thread
1. extend Thread class

2. implement Runnable interface

3. implement Callable interface (requires ExecutorService)


// extending Thread
public class MyClass extends Thread {
@Override
public void run() {
[Link]("Thread '" + getName() + "' is being executed.");
}
public static void main(String[] args) {
new MyClass().start(); we have implemented run(), but we run start()
[Link](
"Thread '" + [Link]().getName() + "' is being executed.");
}
}
Thread 'main' is being executed. Thread 'Thread-0' is being executed.
Thread 'Thread-0' is being executed. OR Thread 'main' is being executed.
// implementing Runnable
public class MyClass implements Runnable {
@Override
public void run() {
[Link](
"Thread '" + [Link]().getName() + "' is being executed.");
}
public static void main(String[] args) {
new Thread(new MyClass()).start(); creating instance of Thread w/ run() implementation
[Link](
"Thread '" + [Link]().getName() + "' is being executed.");
}
}

Thread 'main' is being executed. Thread 'Thread-0' is being executed.


Thread 'Thread-0' is being executed. OR Thread 'main' is being executed.
// implementing Runnable with lambda expression
public class MyClass { no need for implements Runnable when using lambda

public static void main(String[] args) {


new Thread( implementation of run() method
() -> [Link]( "Thread '" + [Link]().getName()
+ "' is being executed."))
.start();

[Link](
"Thread '" + [Link]().getName() + "' is being executed.");
}
}

Thread 'main' is being executed. Thread 'Thread-0' is being executed.


Thread 'Thread-0' is being executed. OR Thread 'main' is being executed.
-> [Link]

-> [Link]

You might also like