| |

VerySource

 Forgot password?
 Register
Search
View: 809|Reply: 5

Ask about multithreading

[Copy link]

1

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 01:40:01
| Show all posts |Read mode
Why are thread stop suspend resumes in multithreading declared by sun as deprecated?
So what should I do to suspend, stop, and restart the scales?
Thank you for your excellent suggestion!
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-6 11:03:01
| Show all posts
Go to read
I can't finish this word
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-6 11:24:01
| Show all posts
From the api documentation:
The stop () method is inherently unsafe. Terminating a thread with Thread.stop will release all monitors it has locked (as a natural consequence of unchecked ThreadDeath exceptions propagating up the stack). If any objects previously protected by these monitors are in an inconsistent state, the corrupted objects will be visible to other threads, which may lead to arbitrary behavior. Many uses of stop should be replaced by code that only modifies certain variables to indicate that the target thread should stop running. The target thread should periodically check the variable and, if the variable indicates that it is to stop running, return in turn from its run method. If the target thread waits for a long time (for example, based on a condition variable), you should use the interrupt method to interrupt the wait.
And suspend resume has a deadlock tendency
The same function can be implemented with wait and notify / notifyAll, but it is recommended to use notifyAll because the thread evoked by notify is uncertain.
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 19:18:02
| Show all posts
The upstairs is pretty good, let me explain in detail:
1. The stop () method is used to terminate all unfinished methods, including the run () method. When a thread is stopped, all locks on the object it released are released. For example: Suppose you design a thread to transfer money from A's account to B. To ensure security, a lock will be used. Suppose you call stop () when the money is withdrawn from A's account and not transferred to B's account The method stops the thread, at which point the state of the object will be inconsistent. Therefore, we do not know when it is safe to call the stop () method, so it is not recommended.
2. The suspend () method is used to block the thread and change the state of the thread to Blocked. The thread will only be restarted when another thread calls the resume () method. However, all threads calling the suspend () method will suspend all, resulting in a deadlock. So the suspend () method is not recommended, and of course resume () is useless!

The correct method is interrupt (), send an interrupt request to a thread, set the interrupt status to true, and throw an InterruptedException when the thread status is blocked. To check if a thread is terminated, use the isinterrupted () method.
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 19:27:01
| Show all posts
The upstairs is pretty good, let me explain in detail:
1. The stop () method is used to terminate all unfinished methods, including the run () method. When a thread is stopped, all locks on the object it released are released. For example: Suppose you design a thread to transfer money from A's account to B. To ensure security, a lock will be used. Suppose you call stop () when the money is withdrawn from A's account and not transferred to B's account The method stops the thread, at which point the state of the object will be inconsistent. Therefore, we do not know when it is safe to call the stop () method, so it is not recommended.
2. The suspend () method is used to block the thread and change the state of the thread to Blocked. The thread will only be restarted when another thread calls the resume () method. However, all threads calling the suspend () method will suspend all, resulting in a deadlock. So the suspend () method is not recommended, and of course resume () is useless!

The correct method is interrupt (), send an interrupt request to a thread, set the interrupt status to true, and throw an InterruptedException when the thread status is blocked. To check if a thread is terminated, use the isinterrupted () method.
Reply

Use magic Report

0

Threads

6

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-10 19:36:01
| Show all posts
The upstairs is pretty good, let me explain in detail:
1. The stop () method is used to terminate all unfinished methods, including the run () method. When a thread is stopped, all locks on the object it released are released. For example: Suppose you design a thread to transfer money from A's account to B. To ensure security, a lock will be used. Suppose you call stop () when the money is withdrawn from A's account and not transferred to B's account The method stops the thread, at which point the state of the object will be inconsistent. Therefore, we do not know when it is safe to call the stop () method, so it is not recommended.
2. The suspend () method is used to block the thread and change the state of the thread to Blocked. The thread will only be restarted when another thread calls the resume () method. However, all threads calling the suspend () method will suspend all, resulting in a deadlock. So the suspend () method is not recommended, and of course resume () is useless!

The correct method is interrupt (), send an interrupt request to a thread, set the interrupt status to true, and throw an InterruptedException when the thread status is blocked. To check if a thread is terminated, use the isinterrupted () method.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list