Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
777 B
0
Indexable
Never
class MyRunnable implements Runnable {
    @Override
    public void run() {
        try {
            System.out.println("Thread started: " + Thread.currentThread().getName());
            Thread.sleep(500);
            System.out.println("Thread ended: " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted: " + Thread.currentThread().getName());
        }
    }
}

public class CreateThreadExample {
    public static void main(String[] args) {
        MyRunnable runnable = new MyRunnable();

        Thread thread1 = new Thread(runnable, "Thread 1");
        Thread thread2 = new Thread(runnable, "Thread 2");

        thread1.start();
        thread2.start();
    }
}