Groovy 线程控制怎么实现?

文章导读
Previous Quiz Next Groovy 提供了对多线程程序的完全控制。您可以开发一个多线程程序,根据需求暂停、恢复或完全停止它。有各种静态方法可用于 thread 对象来控制其行为。
📋 目录
  1. 控制 Groovy 线程的方法
  2. Groovy 中的线程控制示例
A A

Groovy - 线程控制



Previous
Quiz
Next

Groovy 提供了对多线程程序的完全控制。您可以开发一个多线程程序,根据需求暂停、恢复或完全停止它。有各种静态方法可用于 thread 对象来控制其行为。

控制 Groovy 线程的方法

下表列出了在 Groovy 中控制 thread 的方法 −

Sr.No. Method & Description
1

public void suspend()

此方法将 thread 置于挂起状态,并可以使用 resume() 方法恢复。

2

public void stop()

此方法完全停止 thread。

3

public void resume()

此方法恢复使用 suspend() 方法挂起的 thread。

4

public void wait()

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

5

public void notify()

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

Groovy 中的线程控制示例

Example.groovy

class Example {
   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();
         println("暂停第一个线程");
         Thread.sleep(1000);
         r1.resume();
         println("恢复第一个线程");
         
         r2.suspend();
         println("暂停第二个线程");
         Thread.sleep(1000);
         r2.resume();
         println("恢复第二个线程");
      } catch (InterruptedException e) {
         println("主线程被中断");
      }
	  
      try {
         println("等待线程结束。");
         r1.t.join();
         r2.t.join();
      } catch (InterruptedException e) {
         println("主线程被中断");
      }
      println("主线程退出。");
   }
}
class RunnableDemo implements Runnable {
   Thread t;
   private String threadName;
   boolean suspended = false;

   RunnableDemo( String name) {
      threadName = name;
      println("创建 " +  threadName );
   }
   
   void run() {
      println("运行 " +  threadName );
      try {
         for(int i = 10; i > 0; i--) {
            println("线程: " + threadName + ", " + i);
            // 让线程休眠一段时间。
            Thread.sleep(300);
            synchronized(this) {
               while(suspended) {
                  wait();
               }
            }
         }
      } catch (InterruptedException e) {
         println("线程 " +  threadName + " 被中断。");
      }
      println("线程 " +  threadName + " 退出。");
   }

   void start () {
      println("启动 " +  threadName );
      if (t == null) {
         t = new Thread (this, threadName);
         t.start ();
      }
   }
   
   void suspend() {
      suspended = true;
   }
   
   synchronized void resume() {
      suspended = false;
      notify();
   }
}

输出

上述程序将产生以下输出 −

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.