Deadlock is a bug at the operating system level and is the abbreviation of process deadlock. It was first proposed by Dijkstra when studying banker algorithms in 1965. It is one of the most difficult problems in computer operating systems and even the entire concurrent programming field.
In fact, there are many things in the computer world that need to be solved by multiple threads, because in this way, resources can be used to maximize the efficiency of computing. However, in fact, there are many situations in computer systems where resources can only be used by one process at a time, such as printers, and only one process can control it at the same time. In a multi-channel programming environment, several processes often share such resources, and a process may require more than one resource. Therefore, several processes will compete for limited resources and the order of advancement will be improperly, thus forming a situation of indefinite cycle waiting. We call this state deadlock. To put it simply, deadlock refers to a situation where multiple processes cycle through the resources they occupy and remain in a stalemate indefinitely. Obviously, if there is no external force, then all processes involved in deadlock will always be blocked.
File name: DeadThreadByExtend.java
Note:
1. When starting a thread, the start method can be used. The run method can also be called, but it is only equivalent to a normal call and executed in the current thread.
2. Synchronized cannot directly modify variables.
3. The synchronized block does not force single-thread access to variables in the block. It simply means that the synchronized (args) parameter is locked when executing statements within the block and is not released until the execution is finished.
package com.ycf.study.thread;class Sources{int a;public void setA(int x) {synchronized (this) {this.a = x;try {Thread.sleep(2000);}catch (InterruptedException e) {e.printStackTrace();}}}} public class DeadThreadByExtend {public static void main(String[] args) {Sources s1 = new Sources();Sources s2 = new Sources();class MyThread1 extends java.lang.Thread {@Override public void run() {System.out.println("Thread 1 starts");synchronized (s1) {System.out.println("Thread 1 applies to modify s1");s1.setA(20);System.out.println("Thread 1 exits and releases lock++++++++++++++++");}}class MyThread2 extends java.lang.Thread {@Override public void run() {System.out.println("Thread 2 starts");synchronized (s2) {System.out.println("Thread 2 applies to modify s2");s2.setA(20);System.out.println("Thread 2 changes s2 complete");System.out.println("Thread 2 applies to modify s1");s1.setA(10);System.out.println("Thread 2 changes s1 complete");}System.out.println("Thread 2 exits and releases the lock++++++++++++++");}}MyThread1 mt1 = new MyThread1();MyThread2 mt2 = new MyThread2();mt1.start();mt2.start();}}Summarize
The above is all about the Java thread deadlock code example in this article, I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site.