Java 中的 线程控制函数

首页 / Java入门教程 / Java 中的 线程控制函数

核心Java提供了对多线程程序的完全控制,您可以开发一个多线程程序,该程序可以根据您的要求完全挂起,恢复或停止。您可以对线程对象使用各种静态方法来控制其行为。下表列出了这些方法-

Sr.No.Method & Remark
1

public void suspend()

该方法将线程置于挂起状态,可以使用resume()方法恢复该线程。

链接:https://www.learnfk.comhttps://www.learnfk.com/java/java-thread-control.html

来源:LearnFk无涯教程网

2

public void stop()

此方法完全停止线程。

3

public void resume()

此方法恢复一个线程,该线程已使用suspend()方法挂起。

4

public void wait()

使当前线程等待,直到另一个线程调用notify()。

5

public void notify()

唤醒正在此对象的监视器上等待的单个线程。

请注意,最新版本的Java已弃用suspend(),resume()和stop()方法,因此您需要使用可用的替代方法。

class RunnableDemo implements Runnable {
   public Thread t;
   private String threadName;
   boolean suspended = false;

   RunnableDemo( String name) {
      threadName = name;
      System.out.println("Creating " +  threadName );
   }
   
   public void run() {
      System.out.println("Running " +  threadName );
      try {
         for(int i = 10; i > 0; i--) {
            System.out.println("Thread: " + threadName + ", " + i);
            //Let the thread sleep for a while.
            Thread.sleep(300);
            synchronized(this) {
               while(suspended) {
                  wait();
               }
            }
         }
      } 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 ();
      }
   }
   
   void suspend() {
      suspended = true;
   }
   
   synchronized void resume() {
      suspended = false;
      notify();
   }
}

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();

      try {
         Thread.sleep(1000);
         R1.suspend();
         System.out.println("Suspending First Thread");
         Thread.sleep(1000);
         R1.resume();
         System.out.println("Resuming First Thread");
         
         R2.suspend();
         System.out.println("Suspending thread Two");
         Thread.sleep(1000);
         R2.resume();
         System.out.println("Resuming thread Two");
      } catch (InterruptedException e) {
         System.out.println("Main thread Interrupted");
      }try {
         System.out.println("Waiting for threads to finish.");
         R1.t.join();
         R2.t.join();
      } catch (InterruptedException e) {
         System.out.println("Main thread Interrupted");
      }
      System.out.println("Main thread exiting.");
   }
}

上面的程序产生以下输出-

Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 10
Running Thread-2
Thread: Thread-2, 10
Thread: Thread-1, 9
Thread: Thread-2, 9
Thread: Thread-1, 8
Thread: Thread-2, 8
Thread: Thread-1, 7
Thread: Thread-2, 7
Suspending First Thread
Thread: Thread-2, 6
Thread: Thread-2, 5
Thread: Thread-2, 4
Resuming First Thread
Suspending thread Two
Thread: Thread-1, 6
Thread: Thread-1, 5
Thread: Thread-1, 4
Thread: Thread-1, 3
Resuming thread Two
Thread: Thread-2, 3
Waiting for threads to finish.
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
Main thread exiting.

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出gRPC -〔李林锋〕

TensorFlow快速入门与实战 -〔彭靖田〕

编译原理之美 -〔宫文学〕

安全攻防技能30讲 -〔何为舟〕

NLP实战高手课 -〔王然〕

Web安全攻防实战 -〔王昊天〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

结构思考力 · 透过结构看表达 -〔李忠秋〕

Midjourney入门实践课 -〔Jovi〕

好记忆不如烂笔头。留下您的足迹吧 :)