📜  Java-多线程

📅  最后修改于: 2020-11-15 04:34:14             🧑  作者: Mango


Java是一种多线程编程语言,这意味着我们可以使用Java开发多线程程序。多线程程序包含两个或多个可以同时运行的部分,并且每个部分可以同时处理不同的任务,从而可以最佳地利用可用资源,尤其是在计算机具有多个CPU的情况下。

根据定义,多任务是指多个进程共享诸如CPU之类的通用处理资源。多线程将多任务的概念扩展到应用程序中,您可以在其中将单个应用程序内的特定操作细分为各个线程。每个线程可以并行运行。 OS不仅在不同的应用程序之间分配处理时间,还在应用程序内的每个线程之间分配处理时间。

多线程使您可以编写一种方式,使多个活动可以在同一程序中同时进行。

线程的生命周期

线程在其生命周期中会经历各个阶段。例如,线程是先生,启动,运行然后死亡的。下图显示了线程的完整生命周期。

Java线程

以下是生命周期的各个阶段-

  • 新建-新线程以新状态开始其生命周期。它将保持这种状态,直到程序启动线程为止。它也称为出生线程

  • Runnable接口-一个刚出生的线程启动后,该线程进入可运行状态。处于此状态的线程被视为正在执行其任务。

  • 等待中-有时,线程在等待另一个线程执行任务时会转换为等待状态。仅当另一个线程发出信号通知等待的线程继续执行时,一个线程才转换回可运行状态。

  • 定时等待-可运行线程可以在指定的时间间隔内进入定时等待状态。当该时间间隔到期或发生等待事件时,处于此状态的线程将转换回可运行状态。

  • 终止(死) -可运行线程在完成其任务或终止时进入终止状态。

线程优先级

每个Java线程都有一个优先级,可以帮助操作系统确定线程调度的顺序。

Java线程优先级在MIN_PRIORITY(常数1)和MAX_PRIORITY(常数10)之间。默认情况下,每个线程的优先级为NORM_PRIORITY(常数5)。

具有较高优先级的线程对程序更重要,应在较低优先级的线程之前为其分配处理器时间。但是,线程优先级不能保证线程执行的顺序,并且很大程度上取决于平台。

通过实现可运行接口创建线程

如果您的类打算作为线程执行,则可以通过实现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);
            // 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类中的可用方法创建的多个线程时提供了更大的灵活性。

第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);
            // 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.

线程方法

以下是Thread类中可用的重要方法的列表。

Sr.No. Method & Description
1

public void start()

Starts the thread in a separate path of execution, then invokes the run() method on this Thread object.

2

public void run()

If this Thread object was instantiated using a separate Runnable target, the run() method is invoked on that Runnable object.

3

public final void setName(String name)

Changes the name of the Thread object. There is also a getName() method for retrieving the name.

4

public final void setPriority(int priority)

Sets the priority of this Thread object. The possible values are between 1 and 10.

5

public final void setDaemon(boolean on)

A parameter of true denotes this Thread as a daemon thread.

6

public final void join(long millisec)

The current thread invokes this method on a second thread, causing the current thread to block until the second thread terminates or the specified number of milliseconds passes.

7

public void interrupt()

Interrupts this thread, causing it to continue execution if it was blocked for any reason.

8

public final boolean isAlive()

Returns true if the thread is alive, which is any time after the thread has been started but before it runs to completion.

先前的方法在特定的Thread对象上调用。 Thread类中的以下方法是静态的。调用静态方法之一对当前正在运行的线程执行操作。

Sr.No. Method & Description
1

public static void yield()

Causes the currently running thread to yield to any other threads of the same priority that are waiting to be scheduled.

2

public static void sleep(long millisec)

Causes the currently running thread to block for at least the specified number of milliseconds.

3

public static boolean holdsLock(Object x)

Returns true if the current thread holds the lock on the given Object.

4

public static Thread currentThread()

Returns a reference to the currently running thread, which is the thread that invokes this method.

5

public static void dumpStack()

Prints the stack trace for the currently running thread, which is useful when debugging a multithreaded application.

下面的ThreadClassDemo程序演示了Thread类的某些方法。考虑一个实现Runnable的DisplayMessage类-

// File Name : DisplayMessage.java
// Create a thread to implement Runnable

public class DisplayMessage implements Runnable {
   private String message;
   
   public DisplayMessage(String message) {
      this.message = message;
   }
   
   public void run() {
      while(true) {
         System.out.println(message);
      }
   }
}

以下是扩展Thread类的另一个类-

// File Name : GuessANumber.java
// Create a thread to extentd Thread

public class GuessANumber extends Thread {
   private int number;
   public GuessANumber(int number) {
      this.number = number;
   }
   
   public void run() {
      int counter = 0;
      int guess = 0;
      do {
         guess = (int) (Math.random() * 100 + 1);
         System.out.println(this.getName() + " guesses " + guess);
         counter++;
      } while(guess != number);
      System.out.println("** Correct!" + this.getName() + "in" + counter + "guesses.**");
   }
}

以下是使用上述类的主程序-

// File Name : ThreadClassDemo.java
public class ThreadClassDemo {

   public static void main(String [] args) {
      Runnable hello = new DisplayMessage("Hello");
      Thread thread1 = new Thread(hello);
      thread1.setDaemon(true);
      thread1.setName("hello");
      System.out.println("Starting hello thread...");
      thread1.start();
      
      Runnable bye = new DisplayMessage("Goodbye");
      Thread thread2 = new Thread(bye);
      thread2.setPriority(Thread.MIN_PRIORITY);
      thread2.setDaemon(true);
      System.out.println("Starting goodbye thread...");
      thread2.start();

      System.out.println("Starting thread3...");
      Thread thread3 = new GuessANumber(27);
      thread3.start();
      try {
         thread3.join();
      } catch (InterruptedException e) {
         System.out.println("Thread interrupted.");
      }
      System.out.println("Starting thread4...");
      Thread thread4 = new GuessANumber(75);
      
      thread4.start();
      System.out.println("main() is ending...");
   }
}

这将产生以下结果。您可以反复尝试该示例,每次都会得到不同的结果。

输出

Starting hello thread...
Starting goodbye thread...
Hello
Hello
Hello
Hello
Hello
Hello
Goodbye
Goodbye
Goodbye
Goodbye
Goodbye
.......

Java多线程主要概念

在用Java进行多线程编程时,您需要非常方便地使用以下概念-