The code is very simple, the baby is directly on the code, please spray it!
/**
*jdk1.5 after the lock, the creation of locks is not so cumbersome, call lock locked unlock unlocked, more object-oriented
*/
public class Locktest {
public static void Main (string[] args) {
Using the method of inner class to realize runnable
New Thread (New Runnable () {
@Override
public void Run () {
Call the Print method in the Out method
Out pOut = new Out ();
Pout.print ("Hello");
}
Open a thread
}). Start ();
}
}
Class out{
Call Lock's implementation class Renntrantlocak
Lock lock = new Reentrantlock ();
public void print (String name) {
Lock the current name
Lock.lock ();
try {
Print the length of name
for (int i = 0; i < name.length (); i++) {
System.out.println (Name.charat (i));
}
System.out.println ();
}finally{
Unlock
Lock.unlock ();
}
}
}
Print:
H
E
L
L
O
Lock locked unlock Unlocked in Java