java thread deadlock code example

  • 2020-11-20 06:07:22
  • OfStack

Deadlock is an operating system level error, is the abbreviation of process deadlock, first proposed by Dijkstra in 1965 in the study of bankers algorithm, it is the computer operating system and the entire field of concurrent programming most difficult to deal with 1.

In fact, there are many things in the computer world that require multithreading to solve, because this is the most efficient way to utilize resources. In practice, however, there are many situations in a computer system where resources can only be used by one process at a time, such as a printer, and only one process can control it at a time. In a multichannel programming environment, such resources are often Shared by several processes, and more than one resource is likely to be required by one process. As a result, a number of processes compete for limited resources and advance in an improper order, resulting in an indefinite cycle of waiting. We call this state a deadlock. In simple terms, a deadlock is a situation in which multiple process loops wait indefinitely for a resource to be possessed by another party. Obviously, if there is no external force, the processes involved in the deadlock will remain permanently locked.

File name: DeadThreadByExtend.java

Note:

1. The start method is used when starting the thread. The run method can also be called, but it is only equivalent to a normal call and executed within the current thread.

2. synchronized cannot directly modify variables.

3. synchronized blocks do not force single-threaded access to intra-block variables. It simply means that the parameters of synchronized (args) are locked when the in-block statement is executed and are not released until the end of execution.


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 start ");
				synchronized (s1) {
					System.out.println(" thread 1 To apply for modifying s1");
					s1.setA(20);
					System.out.println(" thread 1 Modified to complete ");
					System.out.println(" thread 1 To apply for modifying s2");
					s2.setA(10);
					System.out.println(" thread 1 Modify the s2 complete ");
				}
				System.out.println(" thread 1 Exit and release the lock ++++++++++");
			}
		}
		class MyThread2 extends java.lang.Thread {
			@Override
			      public void run() {
				System.out.println(" thread 2 start ");
				synchronized (s2) {
					System.out.println(" thread 2 To apply for modifying s2");
					s2.setA(20);
					System.out.println(" thread 2 Modify the s2 complete ");
					System.out.println(" thread 2 To apply for modifying s1");
					s1.setA(10);
					System.out.println(" thread 2 Modify the s1 complete ");
				}
				System.out.println(" thread 2 Exit and release the lock ++++++++++");
			}
		}
		MyThread1 mt1 = new MyThread1();
		MyThread2 mt2 = new MyThread2();
		mt1.start();
		mt2.start();
	}
}

conclusion

That's it for the java thread deadlock code example, and I hope you found it helpful. If there is any deficiency, please let me know. Thank you for your support to this site.


Related articles: