Java Threads: Synchronization-synchronized blocks of threads

Source: Internet
Author: User
Tags sleep thread

The fundamental purpose of the synchronization is to control the proper access to competitive resources, so as long as access to competitive resources is guaranteed to only one thread at a time, Java introduces a strategy for synchronizing code fast to improve performance.

On the basis of the last example, the Oper method is changed, and the synchronization method is changed to the synchronized code block mode, and the execution logic of the program is not a problem.

/**


* Java Thread: Synchronization of threads-synchronized code blocks


*


* @author leizhimin


*/


public class Test {


public static void Main (string[] args) {


User U = New User ("John", 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.oper (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 Method


          *


* @param x Add x million


          */


public void oper (int x) {


try {


Thread.Sleep (10L);


synchronized (this) {


This.cash + = x;


System.out.println (Thread.CurrentThread (). GetName () + "run over, add" + x + "", current user account balance is : "+ cash);


                         }


Thread.Sleep (10L);


catch (Interruptedexception e) {


E.printstacktrace ();


                 }


         }


@Override


public String toString () {


return "user{" +


"code= '" + code + ' \ ' +


", cash=" + Cash +


                                 '}';


         }


}

线程E运行结束,增加“32”,当前用户账户余额为:132
线程B运行结束,增加“-60”,当前用户账户余额为:72
线程D运行结束,增加“-30”,当前用户账户余额为:42
线程F运行结束,增加“21”,当前用户账户余额为:63
线程C运行结束,增加“-80”,当前用户账户余额为:-17
线程A运行结束,增加“20”,当前用户账户余额为:3
Process finished with exit code 0

Attention:

When using the Synchronized keyword, you should avoid using the sleep or yield method in the synchronized method or synchronized block as much as possible, because the synchronized program block occupies an object lock, You rest so that other threads can only wait for you to wake up and execute. Not only seriously affect efficiency, but also illogical.

Similarly, it does not make sense to call the Yeild method within the Synchronizer block to give up CPU resources, as you occupy the lock and other mutex threads still cannot access the Synchronizer block. Of course, threads unrelated to the Synchronizer block can get more execution time.

Source: http://lavasoft.blog.51cto.com/62575/221922

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.