Java中Thread类的方法可以通过创建Thread对象来使用。以下是一些常用的Thread类方法:
以下是一个示例代码,展示了如何使用Thread类的方法:
public class MyThread extends Thread {
    public void run() {
        // 线程的执行逻辑
        for (int i = 0; i < 5; i++) {
            System.out.println("Thread " + getName() + ": " + i);
            try {
                sleep(1000); // 线程休眠1秒
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        MyThread thread1 = new MyThread();
        thread1.setName("Thread 1");
        
        MyThread thread2 = new MyThread();
        thread2.setName("Thread 2");
        thread1.start(); // 启动线程1
        thread2.start(); // 启动线程2
        try {
            thread1.join(); // 等待线程1终止
            thread2.join(); // 等待线程2终止
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        System.out.println("Main thread finished");
    }
}
以上代码中,首先创建了两个MyThread对象,并设置它们的名称。然后通过调用start()方法启动线程,并执行run()方法中的逻辑。在main方法中,使用join()方法等待两个线程终止,最后打印出"Main thread finished"。