Java并发主要操作

来源:互联网 发布:薛之谦和李雨桐 知乎 编辑:程序博客网 时间:2024/05/22 04:28

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

编号方法说明描述1public void suspend()该方法使线程处于挂起状态,可以使用resume()方法恢复。2public void stop()该方法使线程完全停止。3public void resume()该方法恢复使用suspend()方法挂起的线程。4public void wait()导致当前线程等到另一个线程调用notify()。5public 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.");   }}
Java

执行上面程序后产生以下输出 -

Creating Thread-1Starting Thread-1Creating Thread-2Starting Thread-2Running Thread-1Thread: Thread-1, 10Running Thread-2Thread: Thread-2, 10Thread: Thread-1, 9Thread: Thread-2, 9Thread: Thread-1, 8Thread: Thread-2, 8Thread: Thread-1, 7Thread: Thread-2, 7Suspending First ThreadThread: Thread-2, 6Thread: Thread-2, 5Thread: Thread-2, 4Resuming First ThreadSuspending thread TwoThread: Thread-1, 6Thread: Thread-1, 5Thread: Thread-1, 4Thread: Thread-1, 3Resuming thread TwoThread: Thread-2, 3Waiting for threads to finish.Thread: Thread-1, 2Thread: Thread-2, 2Thread: Thread-1, 1Thread: Thread-2, 1Thread Thread-1 exiting.Thread Thread-2 exiting.Main thread exiting.
原创粉丝点击