Thread is a very important concept in Java programming, and this article interprets it in detail in the form of an example. The specific analysis is as follows:
First of all, what is the use of line Cheng? For example, you now have 30000 of dollars of ocean in the bank, now you go to the bank to withdraw money, when you enter the password to complete, has entered the amount of withdrawals, such as you enter the 20000, is the bank to give you money at this moment, your wife also went to the bank to take the money, your wife also take 20000, Because at this time your account is still 30000, so the bank of the same operation at your wife that end again, so that when you two complete their respective operations, the bank records you should have 10000 deposit, this is not very cool. Solves this problem to use the line Cheng knowledge, below lets us study together.
One example of unhandled thread synchronization:
public class Textsync implements runnable{
/** unhandled thread sync
* @param args
/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, the first thread pauses for a second, and//the
second thread starts executing, when the second thread executes to this point, Num becomes 2, the second thread pauses for a second,
//The first thread at this time the NUM also becomes 2, So the final result is 2;
thread.sleep (1000);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println (name+) is the "+num+" thread that executes. ");
}
}
Output results:
The T2 is the 2nd executing thread.
the T1 is the 2nd executing thread.
Second, thread synchronization
public class Textsynctwo implements runnable{
/** thread sync
* @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, either declaratively when the method is defined, or set in a method. Public
synchronized void Add (String name) {
//synchronized (this) {//lock current thread to prevent execution of
try {num by another thread at this time
++;
Thread.Sleep (1000);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println (name+) is the "+num+" thread that executes. ");
//}
}
}
Output results:
The T1 is the 1th executing thread.
the T2 is the 2nd executing thread.
Third, 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");
}
}
}
}
}
Four, lock
public class TT implements runnable{
/** lock
* @param args
*
/int b = m;
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;
}
}
Now the output results are:
Visible here M2 first implementation, M1 to wait for M2 execution after completion.
I hope this article will help you with your Java programming.