我们可以通过实现可运行的接口或扩展Thread类来创建Thread。以下是使用两种方式创建线程的详细步骤。
如果您的类打算作为线程执行,则可以通过实现Runnable接口来实现。您将需要遵循三个基本步骤-
步骤1
第一步,您需要实现Runnable接口run()
提供的方法。该方法为线程提供了一个入口点,您将把完整的业务逻辑放入该方法中。以下是方法的简单语法-run()
public void run( )
第2步
第二步,您将使用以下构造函数实例化Thread对象-
Thread(Runnable threadObj, String threadName);
其中,threadObj是实现Runnable接口的类的实例,而threadName是赋予新线程的名称。
第三步
创建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); //让线程休眠一会儿。 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类中的可用方法创建的多个线程时提供了更大的灵活性。
步骤1
您将需要重写Thread类中可用的run()方法。该方法为线程提供了一个入口点,您将把完整的业务逻辑放入该方法中。以下是run()
方法的简单语法-
public void run( )
第2步
创建线程对象后,可以通过调用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); //让线程休眠一会儿。 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.