Multithreading in Java (10)

本文介绍Java中如何通过自定义方法实现线程的暂停与恢复操作,包括线程状态控制变量的使用,以及如何同步这些操作来避免死锁等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Multithreading in Java - Suspending and Resuming Threads


(Page 10 of 10 )

There might be times when you need to temporarily stop a thread from processing and then resume processing, such as when you want to let another thread use the current resource. You can achieve this objective by defining your own suspend and resume methods, as shown in the following example.

This example defines a MyThread class. The MyThread class defines three methods: the run() method, the suspendThread() method, and the resumeThread() method. In addition, the MyThread class declares the instance variable suspended , whose value is used to indicate whether or not the thread is suspended.

The run() method contains a for loop that displays the value of the counter variable. Each time the counter variable is displayed, the thread pauses briefly. It then enters a synchronized statement to determine whether the value of the suspended instance variable is true. If so, the wait() method is called, causing the thread to be suspended until the notify() method is called.

The suspendThread() method simply assigns true to the suspended instance variable. The resumeThread() method assigns false to the suspended instance variable and then calls the notify() method. This causes the thread that is suspended to resume processing.

The main() method of the Demo class declares an instance of MyThread and then pauses for about a second before calling the suspendThread() method and displaying an appropriate message on the screen. It then pauses for about another second before calling the resumeThread() method and again displaying an appropriate message on the screen.

The thread continues to display the value of the counter variable until the thread is suspended. The thread continues to display the value of the counter variable once the thread resumes processing. Here’s what is displayed when you run this program:

Thread: 0
Thread: 1
Thread: 2
Thread: 3
Thread: 4
Thread: Suspended
Thread: Resume
Thread: 5
Thread: 6
Thread: 7
Thread: 8
Thread: 9
Thread exiting.
class MyThread implements Runnable {
  String name;
  Thread t;
  boolean suspended;
  MyThread() {
    
t = new Thread(this, "Thread");
     suspended = false ;
     t.start();
 
}
  public void run() {
     try {
       
for (int i = 0; i < 10; i++) {
           System.out.println("Thread: " + i ); 
           Thread.sleep(200);
           synchronized (this) {
             
while (suspended) {
                 wait();
              }
           }
        }
     } catch (InterruptedException e ) {
         
System.out.println("Thread: interrupted.");
     }
     System.out.println("Thread exiting.");
 
}
  void suspendThread() {
    
suspended = true;
  }
  synchronized void resumeThread() {
    
suspended = false;
     notify();
 
}
}
class Demo {
  
public static void main (String args [] ) {
      MyThread t1 = new MyThread();
      try{
        
Thread.sleep(1000);
         t1.suspendThread();
         System.out.println("Thread: Suspended"); 
         Thread.sleep(1000);
         t1.resumeThread();
         System.out.println("Thread: Resume");
       } catch ( InterruptedException e) {
       }
       try {
         
t1.t.join();
       } catch ( InterruptedException e) {
            System.out.println (
                 "Main Thread: interrupted");
       }
   }
}

Quiz
  1. What is a thread?
  2. What is multitasking?
  3. What kind of overhead occurs during multitasking?
  4. What is a thread priority?
  5. What is synchronization?
  6. What is the Runnable interface ?
  7. When should you extend the Thread class?
  8. If you create one thread in your program, how many threads actually run?
  9. What method must you override when you create a thread in your program?
  10. How do you define the portion of your program that becomes a thread?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值