Java thread: Synchronous thread-synchronous Block

Source: Internet
Author: User
In addition to the synchronization method, you can also use the synchronization code block. Sometimes, the synchronization code block will bring better results than the synchronization method. The fundamental purpose of synchronization is to control the correct access to competing resources. Therefore, only one thread can access competing resources at the same time, therefore, Java introduces a fast synchronization code policy to improve performance. Based on the previous example, the merge method was changed from the synchronous method to the synchronous code block mode, and the execution logic of the program was no problem. /**
* Java thread: synchronous-synchronous code block of a thread
*
* @ Author leizhimin 2009-11-4 11:23:32
*/
Public class Test {
Public static void main (String [] args ){
User u = new User ("James", 100 );
MyThread t1 = new MyThread ("thread A", u, 20 );
MyThread t2 = new MyThread ("thread B", u,-60 );
Mythread T3 = new mythread ("thread C", U,-80 );
Mythread t4 = new mythread ("thread D", U,-30 );
Mythread T5 = new mythread ("thread E", U, 32 );
Mythread T6 = new mythread ("thread F", U, 21 );

T1.start ();
T2.start ();
T3.start ();
T4.start ();
T5.start ();
T6.start ();
}
}

Class MyThread extends Thread {
Private User u;
Private int y = 0;

Mythread (string name, user U, int y ){
Super (name );
This. U = u;
This. Y = y;
}

Public void run (){
U. Random (y );
}
}

Class user {
Private string code;
Private int cash;

User (String code, int cash ){
This. code = code;
This. cash = cash;
}

Public String getCode (){
Return code;
}

Public void setCode (String code ){
This. Code = code;
}

/**
* Business methods
*
* @ Param X Add x million RMB
*/
Public void merge (int x ){
Try {
Thread. Sleep (10l );
Synchronized (this ){
This. cash + = x;
System. out. println (Thread. currentThread (). getName () + "run ended. Add" + x + "". The current user account balance is: "+ cash );
}
Thread. Sleep (10l );
} Catch (interruptedexception e ){
E. printstacktrace ();
}
}

@ Override
Public String tostring (){
Return "User {" +
"Code = '" + code +'/''+
", Cash =" + cash +
'}';
}
} Thread E is running and "32" is added. The current user account balance is 132.
Thread B stops running and adds "-60". The current user account balance is 72.
Thread D stops running and adds "-30". The current user account balance is: 42.
Thread F stops running and adds "21". The current user account balance is: 63
Thread C stops running and adds "-80". The current user account balance is-17.
Thread A stops running and adds "20". The current user account balance is: 3

Process finished with exit code 0

Note: when using the synchronized keyword, try to avoid using the sleep or yield method in the synchronized method or synchronized block, because the synchronized block occupies the object lock, when you rest, other threads can only be executed after you wake up. It not only seriously affects efficiency, but also is not logical. Similarly, it is meaningless to call the yeild method in the synchronization program block to make the CPU resources available. Because you occupy the lock, other mutex threads still cannot access the synchronization program block. Of course, threads unrelated to the synchronization program block can get more execution time.
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.