Creating Threads
There are two ways to create a new thread of execution.
One way is to declare a class as a subclass of Thread.
The recommended is to implement Runnable.
Extending Thread class
The Thread class implements Runnable interface.
The subclass should override the run method of class Thread.
package threads.creating_threads;
public class Extending {
public static void main(String[] args) {
String name = Thread.currentThread().getName();
System.out.println(name);
Thread w1 = new Worker();
Thread w2 = new Worker();
Thread w3 = new Worker();
w1.start();
w2.start();
w3.start();
Runnable task = () -> {
System.out.println(Thread.currentThread().getName());
};
Thread t = new Thread(task, "Task-1");
t.start();
}
}
class Worker extends Thread {
@Override
public void run() {
try {
String threadName = Thread.currentThread().getName();
System.out.println(threadName);
Thread.sleep(1000);
System.out.println(threadName);
Thread.sleep(1000);
} catch (InterruptedException ex) {}
}
}
Implementing Runnable
The recommended alternativ (modern Java) is to implement Runnable directly.
This shows true concurrency - orders are prepared in parallel, not sequentially.
package threads.creating_threads;
public class Implementing {
public static void main(String[] args) {
Runnable order1 = new OrderPreparation("Pizza");
Runnable order2 = new OrderPreparation("Pasta");
Runnable order3 = new OrderPreparation("Salad");
Thread worker1 = new Thread(order1, "Chef-1");
Thread worker2 = new Thread(order2, "Chef-2");
Thread worker3 = new Thread(order3, "Chef-3");
worker1.start();
worker2.start();
worker3.start();
}
}
class OrderPreparation implements Runnable {
private final String orderName;
public OrderPreparation(String orderName) {
this.orderName = orderName;
}
@Override
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + " is preparing " + orderName);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(threadName + " finished " + orderName);
}
}
Thread Stack
Threads are lightweight units of execution within a program.
Multiple threads can run concurrently within the same program.
Threads share the same memory and resources.
Threads allows tasks to be done in parallel or more efficiently.
A thread stack si the private memory area used by a single thread to store method calls.
Threads can execute methods independently without interfering with each other's local variables.