Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Author’s other platforms:

| CSDN:blog.csdn.net/qq_4115394…

| the nuggets: juejin. Cn/user / 651387…

| zhihu: www.zhihu.com/people/1024…

| GitHub:github.com/JiangXia-10…

| public no. : 1024 notes

This article is about 3,707 words and takes 10 minutes to read

1 introduction

The previous article on stopping a thread in multithreaded programming described three ways to stop a thread from running:

1. Use exit flag to make thread exit normally;

2. Use the stop method to force the thread to end. This method has been abandoned because it is unsafe.

Use the interrupt method to interrupt the thread.

In multithreaded programming, in addition to stopping a thread as described above, you can also use several methods to suspend a running thread. If a suspended thread can use the suspend method, which means it can resume running, the thread that restarts the pause line can use the resume method. But both methods are expired, just like the stop method.

2 the body

Take a look at the suspend and resume methods in use with an example:

* author: Jiangxia * Date: 2021-04-16 */ public class Demo19 { public static void main(String[] args) throws InterruptedException { Thread19 t = new Thread19(); // Start the thread with the start method. Thread.sleep(1000); // The resume method is also discarded. System.out.println("Time1="+ System.currentTimemillis ()+"; i="+t.getI()); Thread.sleep(1000); System.out.println("Time1="+System.currentTimeMillis()+"; i="+t.getI()); // Resume method is also deprecated. Thread.sleep(1000); t.suspend(); System.out.println("Time2="+System.currentTimeMillis()+"; i="+t.getI()); Thread.sleep(1000); System.out.println("Time2="+System.currentTimeMillis()+"; i="+t.getI()); } } class Thread19 extends Thread{ private long i=0; public long getI() { return i; } public void setI(long i) { this.i = i; } @Override public void run() { while(true){ i++; }}}Copy the code

Although threads are now successfully suspended and resumed, we find that suspend and resume have long been marked as deprecated methods and are not recommended. IDEA editor delete line display:

So why was it abandoned?

It must be functionally defective. The flaw is that the lock is not released. If the target thread that called the suspend method holds a lock on an important system resource while it is suspended, it cannot be accessed by any other thread until the target thread restarts.

* author: Jiangxia * Date: 2021-04-16 */ public class Demo20 { public static void main(String[] args) throws InterruptedException { Demo20Service demo20Service = new Demo20Service(); Thread t1 = new Thread(){ @Override public void run() { demo20Service.printInfo(); }}; t1.setName("A"); t1.start(); Thread.sleep(10); Thread t2 = new Thread(){@override public void run() {demo20service.printinfo (); }}; t2.start(); }} class Demo20Service{synchronized public void printInfo(){system.out.println (" thread start "); If ("A".equals(thread.currentThread ().getName()))){system.out.println ("A Thread suspend"); Thread.currentThread().suspend(); } system.out.println (" thread terminated "); }}Copy the code

Also pay attention to the synchronization of system.out.print (). Suspend the println method does not release the synchronization lock. The lock is not released when the program runs inside the println method. As a result, the println method of the current PrintStream object is “suspended”.

* author: Jiangxia * Date: 2021-04-16 */ public class Demo21 { public static void main(String[] args) throws InterruptedException { Thread thread =  new Demo21Thread(); thread.start(); Thread.sleep(100); thread.suspend(); System.out.println("main thread ends "); } } class Demo21Thread extends Thread{ private long i=0; @Override public void run() { while (true){ i++; System.out.println("i="+i); }}}Copy the code

You can see that the result does not print the end of the main thread. Println = println;

public void println(String x) { synchronized (this) { print(x); newLine(); }}Copy the code

Because the println method is a synchronous method, the lock is not released after the suspend thread.

Another drawback is the lack of synchronization. The suspend and resume methods are also prone to data asynchronism due to thread pauses. Such as:

package com.jiangxia.chap1; * author: Jiangxia * date: 2021-04-16 */ public class Demo22 { public static void main(String[] args) throws InterruptedException { Demo22User user  = new Demo22User(); Thread t1 = new Thread(){@override public void run() {user.updateuser (" ", ""); }}; t1.setName("A"); t1.start(); Thread.sleep(10); Thread t2 = new Thread(){ @Override public void run() { user.printUserInfo(); }}; t2.start(); }} class Demo22User {private String name = "Demo22User "; Private String gen = "male "; public void updateUser(String name, String gen){ this.name = name; If ("A".equals(thread.currentThread ().getName())){system.out.println (" stop Thread "); Thread.currentThread().suspend(); } this.gen = gen; } public void printUserInfo(){system.out.println (" name =" + name + ", gender =" +gen); }}Copy the code

It can be found that the output should be diao Chan female, the output is diao Chan male. The results of the program run out of sync.

3 summary

The suspend() method suspends a thread without releasing a synchronization lock, and the suspension does not result in an increase in sleep time;

Resume () restores the thread to its state and continues execution of the remaining code before it paused.

However, the above two methods are expired methods, so they are not recommended.

Related recommendations:

  • Several methods of stopping threads in multithreaded programming

  • Common apis for multithreaded programming threads

  • Threads and common methods for threads

  • Six states of a thread