Does Java single thread cause deadlock?
This article mainly introduces "will Java single thread cause deadlock". In daily operation, I believe that many people have doubts about whether Java single thread will cause deadlock. The editor has consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the question of "will Java single thread lead to deadlock?" Next, please follow the editor to study!
Deadlock theoretically states that there are two threads, which can be thought of as two people, An and B, waiting for B to finish something, and B waiting for A to finish something.
Is it possible for a single-threaded function in the code to cause a deadlock?
Let's look at the following code
# include#include#include#include#include pthread_mutex_t mutex;int main () {pthread_mutex_init (& mutex,NULL); printf ("mutex init\ n"); pthread_mutex_lock (& mutex); printf ("mutex lock#1\ n"); pthread_mutex_lock (& mutex); printf ("mutex lock#2\ n"); pthread_mutex_unlock (& mutex); pthread_mutex_unlock (& mutex) Pthread_mutex_destroy (& mutex); return 0;}
Do you think this kind of code will lead to deadlock?
Will there be a situation in which the thread continues to try to lock immediately after it is locked?
Code like this
Int test_function (void) {/ / lock do_something () if (x) return-1 / / unlock}
After locking, the following will quit because of some judgment, and the next time it comes in, it will be locked again. This kind of deadlock is caused by incorrect code logic, and it is also a common problem for many beginners.
What if it's a two-thread deadlock?
# include # include pthread_mutex_t glistening mutexlockwitch pthread * func1 (void* args) {printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__); pthread_mutex_lock (& g_mutex_lock); sleep (1); pthread_mutex_lock (& g_mutex_lock2) Printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__); pthread_mutex_unlock (& g_mutex_lock); pthread_mutex_unlock (& g_mutex_lock2); return NULL;} void* func2 (void* args) {printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__); pthread_mutex_lock (& g_mutex_lock2); sleep (1) Pthread_mutex_lock (& g_mutex_lock); printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__); pthread_mutex_unlock (& g_mutex_lock2); return NULL;} int main (void) {int ret; pthread_t thread_1; pthread_t thread_2; printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__) Ret = pthread_mutex_init (& g_mutex_lock, NULL); if (ret! = 0) {printf ("mutex init failed\ n"); return-1;} ret = pthread_mutex_init (& g_mutex_lock2, NULL); if (ret! = 0) {printf ("mutex2 init failed\ n"); return-1;} pthread_create (& thread_1, NULL, func1, NULL) Pthread_create (& thread_2, NULL, func2, NULL); pthread_join (thread_1, NULL); pthread_join (thread_2, NULL); pthread_mutex_destroy (& g_mutex_lock); pthread_mutex_destroy (& g_mutex_lock2); printf ("% s (), LINE=%d\ n", _ _ FUNCTION__,__LINE__); return 0;}
The above code is more typical.
After thread 1 gets lock 1, it starts to wait for lock 2, and thread 2 starts to wait for lock 1 after getting lock 2. They belong to the situation of waiting for each other.
Code output
Linux@ubuntu:/study$ gcc argc.c-pthread & &. / a.out
Main (), LINE=37
Func1 (), LINE=11
Func2 (), LINE=23
Finally, a deadlock on a thread does not normally result in a crash unless there are some null pointers on the code because of the lock.
So a single thread can also cause a deadlock.
At this point, the study on "will Java single thread cause deadlock?" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!