Mutex techniques for traditional threads:
Keyword: Synchronized
Example:
public class Traditionalthreadsynchronized {
public static void Main (string[] args) {
New Traditionalthreadsynchronized (). Init ();
}
private void init () {
Final output output = new output ();
New Thread (New Runnable () {
@.....
public void Run () {
while (true) {
Try () {
Thread.Sleep (100);
}catch () {
E.printstracktrace ();
}
Output.outputer ("Zhangsan");
}
}
}). Start ();
New Thread (New Runnable () {
@.....
public void Run () {
while (true) {
Try () {
Thread.Sleep (100);
}catch () {
E.printstracktrace ();
}
Output.outputer ("Zhangsan");
}
}
}). Start ();
}
Class output{
public void Outputer (String name) {
int len = Name.length ();
Synchronized (this) {
for (int i = 0; i< len; i + +) {
System.out.println (Name.charat (i));
}
}
Syso ();
}
}
}
Note: The lock of the static method is the byte code of the current class.
Traditional thread Mutex