Threads are a very important concept in Java programming. This article will explain it in detail in the form of examples. The specific analysis is as follows:
First of all, what is the use of thread locking? For example: For example, if you have 30,000 yuan in the bank now, and you go to the bank to withdraw the money, after you enter the password, you have already entered the withdrawal amount. For example, if you entered 20,000, it means that the bank will get the money for you. At that time, your wife also went to the bank to withdraw the money, and you My wife also withdraws 20,000, because your account is still 30,000 at this time, so the bank performs the same operation on your wife's end. In this way, after you two complete your respective operations, the bank should still have 30,000 recorded in your account. A deposit of 10,000 yuan, isn’t this great? To solve this problem, the knowledge of thread locking is used. Let us learn it together.
1. An example of unhandled thread synchronization:
public class TextSync implements Runnable{ /**Unhandled thread synchronization* @param args */ Time time = new Time(); public static void main(String[] args) { TextSync text = new TextSync(); Thread t1 = new Thread(text); Thread t2 = new Thread(text); t1.setName("t1"); t2.setName("t2"); t1.start(); t2.start(); } @Override public void run() { time.add(Thread.currentThread().getName()); }}class Time { private static int num = 0; public void add(String name){ try { num++; //When the first thread executes to this point, num becomes 1, and the first thread pauses for one second. //The second thread starts executing. When the second thread executes to this point, num It becomes 2, and the second thread pauses for one second. //The num of the first thread also becomes 2 at this time, so the final result is 2; Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println (name+" is the "+num+"th thread of execution."); }}Output result:
t2 is the second thread of execution. t1 is the second thread of execution.
2. Thread synchronization
public class TextSynctwo implements Runnable{ /**Thread synchronization* @param args */ Time1 time = new Time1(); public static void main(String[] args) { TextSynctwo text = new TextSynctwo(); Thread t1 = new Thread( text); Thread t2 = new Thread(text); t1.setName("t1"); t2.setName("t2"); t1.start(); t2.start(); } @Override public void run() { time.add(Thread.currentThread().getName()); }}class Time1 { private static int num = 0; // synchronized locks the current thread, which can be declared when the method is defined, or set in the method. public synchronized void add(String name){ //synchronized (this) {//Lock the current thread to prevent it from being executed by other threads try { num++; Thread.sleep(1000); } catch (InterruptedException e) { e. printStackTrace(); } System.out.println(name+"is the "+num+"th thread of execution."); //} }}Output result:
t1 is the first thread of execution. t2 is the second thread of execution.
3. Deadlock
public class TestDeadLock implements Runnable{ /**Deadlock* @param args */ private int flag = 0; static Object o1 = new Object(); static Object o2 = new Object(); public static void main(String[] args ) { TestDeadLock td1 = new TestDeadLock(); TestDeadLock td2 = new TestDeadLock(); td1.flag = 1; td2.flag = 2; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); t1.setName("t1"); t2.setName("t2"); t1.start() ; t2.start(); } @Override public void run() { System.out.println(Thread.currentThread().getName()); if(flag == 1){ synchronized(o1){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace( ); } synchronized(o2){ System.out.println("1"); } } } if(flag == 2){ synchronized(o2){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized(o1){ System.out.println("2"); } } } }}4. Lock
public class TT implements Runnable{ /**Lock* @param args */ int b = 100; public static void main(String[] args) { TT tt = new TT(); Thread th = new Thread(tt); th .start(); try { tt.m2(); } catch (Exception e) { e.printStackTrace(); } System.out.println(tt.b); } @Override public void run() { try { m1(); } catch (Exception e) { e.printStackTrace(); } } private synchronized void m1() throws Exception{ b = 1000; Thread.sleep(5000); System.out.println("b="+b); } private synchronized void m2() throws Exception{ Thread.sleep(2500); b = 2500; } }The output now is:
1000b=1000
It can be seen that m2 is executed first, and m1 cannot be executed until m2 is completed.
I hope this article will be helpful to everyone’s Java programming design.