Lock locked unlock Unlocked in Java

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.