开发者

Reason behind deprecating the Thread's stop(), destroy(), resume() methods

开发者 https://www.devze.com 2023-04-09 08:08 出处:网络
I read that the Thread class stop(),destroy(),resume() methods been depricated. As a reason it says: \"Because stoppi开发者_运维问答ng a thread in this manner is unsafe and can leave your application

I read that the Thread class stop(),destroy(),resume() methods been depricated. As a reason it says:

"Because stoppi开发者_运维问答ng a thread in this manner is unsafe and can leave your application and the VM in an unpredictable state"

Honestly I didn't understand the reason perfectly from the text. Can someone explain it more clearly please (Perticularly that "VM unpredictable" and "unsafe" words )

Thanks


Because it unlocks all Monitors the Thread has locked, and so other Threads may access an Object with an inconsistent state.

For further infos:

http://download.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html


This article http://developer.android.com/resources/articles/painless-threading.html explains the threading model used in Android. Quoting from that page

the Android UI toolkit is not thread-safe

Meaning if you are doing a lot of thread manipulation you can run into some pretty weird, seemingly random bugs.

Instead you should be using AsyncTasks to accomplish things that take too long for the UI thread. This would include things like saving to and SD card, accessing a database, or sending a network request. For more info on AsyncTasks see the article I linked to above as well as http://developer.android.com/reference/android/os/AsyncTask.html


stop() and resume() have been deprecated in Java for a decade or so. destroy() has been deprecated in Java since Java 1.5. Dalvik is simply mirroring Java's capabilities.

0

精彩评论

暂无评论...
验证码 换一张
取 消