Java中实现Runnable接口与继承Thread类
我们可以通过实现Runnable接口或继承Thread类来创建线程。以下是使用这两种方法创建线程的详细步骤。
通过实现Runnable接口创建线程
如果您的类旨在作为线程执行,则可以通过实现**Runnable**接口来实现此目的。您需要遵循三个基本步骤:
步骤1
第一步,您需要实现**Runnable**接口提供的run()方法。此方法为线程提供了一个入口点,您将在该方法中放入完整的业务逻辑。以下是run()方法的简单语法:
public void run( )
步骤2
第二步,您将使用以下构造函数实例化一个**Thread**对象:
Thread(Runnable threadObj, String threadName);
其中,threadObj是实现**Runnable**接口的类的实例,**threadName**是赋予新线程的名称。
步骤3
创建Thread对象后,您可以通过调用**start()**方法启动它,该方法会执行对run()方法的调用。以下是start()方法的简单语法:
void start();
示例
这是一个创建新线程并开始运行它的示例:
class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. Thread.sleep(50); } } catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } public class TestThread { public static void main(String args[]) { RunnableDemo R1 = new RunnableDemo( "Thread-1"); R1.start(); RunnableDemo R2 = new RunnableDemo( "Thread-2"); R2.start(); } }
这将产生以下结果:
输出
Creating Thread-1 Starting Thread-1 Creating Thread-2 Starting Thread-2 Running Thread-1 Thread: Thread-1, 4 Running Thread-2 Thread: Thread-2, 4 Thread: Thread-1, 3 Thread: Thread-2, 3 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-1, 1 Thread: Thread-2, 1 Thread Thread-1 exiting. Thread Thread-2 exiting.
通过继承Thread类创建线程
创建线程的第二种方法是创建一个扩展**Thread**类的新类,使用以下两个简单的步骤。这种方法在处理使用Thread类中可用方法创建的多个线程时提供了更大的灵活性。
步骤1
您需要重写Thread类中可用的**run( )**方法。此方法为线程提供了一个入口点,您将在该方法中放入完整的业务逻辑。以下是run()方法的简单语法:
public void run( )
步骤2
创建Thread对象后,您可以通过调用**start()**方法启动它,该方法会执行对run()方法的调用。以下是start()方法的简单语法:
void start( );
示例
以下是重写为扩展Thread类的前面程序:
class ThreadDemo extends Thread { private Thread t; private String threadName; ThreadDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. Thread.sleep(50); } } catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } } public class TestThread { public static void main(String args[]) { ThreadDemo T1 = new ThreadDemo( "Thread-1"); T1.start(); ThreadDemo T2 = new ThreadDemo( "Thread-2"); T2.start(); } }
这将产生以下结果:
输出
Creating Thread-1 Starting Thread-1 Creating Thread-2 Starting Thread-2 Running Thread-1 Thread: Thread-1, 4 Running Thread-2 Thread: Thread-2, 4 Thread: Thread-1, 3 Thread: Thread-2, 3 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-1, 1 Thread: Thread-2, 1 Thread Thread-1 exiting. Thread Thread-2 exiting.
广告