Threading is a very important concept in Java programming, and 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, you have 30,000 yuan of ocean deposited in the bank. Now you go to the bank to withdraw money. After you enter the password, you have entered the withdrawal amount. For example, you enter 20,000, and at the moment when the bank gets you money, your wife also goes to the bank to withdraw the money. Your wife also withdraws 20,000, because at this time your account is still 30,000, so the same operation of the bank is carried out again on your wife's side. In this way, when you two complete their respective operations, you should have 10,000 yuan in your account recorded by the bank. Isn't that great? To solve this problem, we use the knowledge of thread locking. Let’s learn it together.
1. An example of unprocessed thread synchronization:
public class TextSync implements Runnable{ /**Not processed 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"); ("t2"); (); (); } @Override public void run() { (().getName()); } } class Time { private static int num = 0; public void add(String name){ try { num++; //When the first thread executes at this point, num becomes 1, and the first thread pauses for one second. //The second thread starts executing. When the second thread executes at this point, num becomes 2, and the second thread pauses for one second. //The num of the first thread also becomes 2, so the final results are 2; (1000); } catch (InterruptedException e) { (); } (name+"Yes"+num+"Execution threads."); } }
Output result:
t2Yes2Execution threads。 t1Yes2Execution threads。
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"); ("t2"); (); (); } @Override public void run() { (().getName()); } } class Time1 { private static int num = 0; //synchronized locks the current thread, 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 at this time try { num++; (1000); } catch (InterruptedException e) { (); } (name+"Yes"+num+"Execution threads."); //} } }
Output result:
t1Yes1Execution threads。 t2Yes2Execution threads。
3. Deadlock
public class TestDeadLock implements Runnable{ /**Dead Lock * @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(); = 1; = 2; Thread t1 = new Thread(td1); Thread t2 = new Thread(td2); ("t1"); ("t2"); (); (); } @Override public void run() { (().getName()); if(flag == 1){ synchronized(o1){ try { (5000); } catch (InterruptedException e) { (); } synchronized(o2){ ("1"); } } } if(flag == 2){ synchronized(o2){ try { (5000); } catch (InterruptedException e) { (); } synchronized(o1){ ("2"); } } } } }
4. Lock
public class TT implements Runnable{ /**locking * @param args */ int b = 100; public static void main(String[] args) { TT tt = new TT(); Thread th = new Thread(tt); (); try { tt.m2(); } catch (Exception e) { (); } (); } @Override public void run() { try { m1(); } catch (Exception e) { (); } } private synchronized void m1() throws Exception{ b = 1000; (5000); ("b="+b); } private synchronized void m2() throws Exception{ (2500); b = 2500; } }
The output result is now:
1000 b=1000
It can be seen that m2 is executed first, and m1 must wait until m2 is executed before it can be executed.
I hope this article will be helpful to everyone's Java programming