Java并发 专题
您的位置:java > Java并发专题 > Java并发主要操作
Java并发主要操作
作者:--    发布时间:2019-11-20

核心java提供对多线程程序的完全控制。 也可以开发一个可以根据您的要求完全暂停,恢复或停止的多线程程序。 有各种静态方法可以用于线程对象来控制它们的行为。 下表列出了这些方法 -

编号 方法 说明描述
1 public void suspend() 该方法使线程处于挂起状态,可以使用resume()方法恢复。
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.

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册