Java Multithreading: Processes, Threads, Creation Methods, and Common Controls
This article explains the fundamental concepts of processes and threads, uses a gaming analogy to illustrate their relationship, and details three ways to create threads in Java—extending Thread, implementing Runnable, and implementing Callable—along with common thread control methods such as sleep, join, and setDaemon.
Process and Thread Basics
The article starts by defining a process as the operating‑system unit that schedules resources and a thread as a sub‑task of a process that the CPU schedules. A gaming analogy is used: a whole game match represents a process, while heroes, minions, or jungle monsters represent individual threads. It highlights that a process can contain multiple threads (multithreading), threads share data within the same process, and processes have isolated memory.
Three Ways to Create Threads in Java
1. Extending Thread Class
Define a subclass of Thread and override run() :
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(getName() + ":打了" + i + "个小兵");
}
}
}Test code creates three instances, sets names, and starts them:
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.setName("鲁班");
t2.setName("刘备");
t3.setName("亚瑟");
t1.start();
t2.start();
t3.start();2. Implementing Runnable Interface
Create a class that implements Runnable and overrides run() :
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "打了:" + i + "个小兵");
}
}
}Test code wraps the runnable in Thread objects with custom names:
MyRunnable mr = new MyRunnable();
Thread t1 = new Thread(mr, "张飞");
Thread t2 = new Thread(mr, "貂蝉");
Thread t3 = new Thread(mr, "吕布");
t1.start();
// optional join to wait for t1 to finish
try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); }
t2.start();
t3.start();3. Implementing Callable Interface
Callable allows a thread to return a result. Example:
public class CallerTask implements Callable
{
public String call() throws Exception {
return "Hello,i am running!";
}
public static void main(String[] args) {
FutureTask
task = new FutureTask<>(new CallerTask());
new Thread(task).start();
try {
String result = task.get();
System.out.println(result);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}Common Thread Control Methods
sleep()
Pauses the current thread for a specified number of milliseconds. Must handle InterruptedException :
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}join()
Causes the calling thread to wait until the target thread finishes execution.
setDaemon()
Marks a thread as a daemon (background) thread, which does not prevent the JVM from exiting when all user threads finish.
Thread t1 = new Thread(mr, "张飞");
Thread t2 = new Thread(mr, "貂蝉");
Thread t3 = new Thread(mr, "吕布");
t1.setDaemon(true);
t2.setDaemon(true);
t1.start();
t2.start();
t3.start();Frequently Asked Questions
Why override run() ? The default run() in Thread does nothing; overriding provides the actual task for the thread.
Difference between run() and start() ? Calling run() executes the method like a normal function on the current thread, while start() creates a new OS thread and then invokes run() on that new thread.
Thread vs. Runnable vs. Callable? Extending Thread ties your class to a single inheritance hierarchy. Implementing Runnable avoids this limitation and separates task logic from thread management. Callable is similar to Runnable but can return a result and throw checked exceptions.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.