Java multithreading - synchronization of threads (synchronized code blocks)

For synchronization, in addition to synchronized methods, you can also use synchronized code blocks, and sometimes synchronized code blocks can bring better results than synchronized methods.

The fundamental purpose of chasing its synchronization is to control the correct access of competing resources, so as long as it is guaranteed that only one thread can access the competing resources at the same time, Java has introduced a strategy of synchronizing code fast to improve performance.

On the basis of the previous example, the oper method has been changed from the synchronous method to the synchronous code block mode. There is no problem with the execution logic of the program.

copy code
package cn.thread;

/**
 * Thread synchronization method
 *
 * @author Lin Jiqin
 * @version 1.0 2013-7-24 上午10:12:47
 */
public class ThreadSynchronizedCode {
    public static void main(String[] args) {
        ThreadSynchronizedCode t = new ThreadSynchronizedCode();
        User u = t.new User("张三", 100);
        MyThread t1 = t.new MyThread("线程A", u, 20);
        MyThread t2 = t.new MyThread("线程B", u, -60);
        MyThread t3 = t.new MyThread("线程C", u, -80);
        MyThread t4 = t.new MyThread("线程D", u, -30);
        MyThread t5 = t.new MyThread("线程E", u, 32);
        MyThread t6 = t.new MyThread("线程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 {
         /** Account */ 
        private String code;
         /** Balance */ 
        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;
        }

        /**
         * deposit
         *
         * @param x deposit amount
         *            
         */
        public void oper(int x) {
            try {
                Thread.sleep(10L);
                synchronized (this) {
                    this.cash += x;
                    System.out.println( "Thread" + Thread.currentThread().getName() + "End of operation, 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 + '}';
        }
    }
}
copy code
Thread thread B runs over, add "-60", the current user account balance is: 40 
Thread thread A runs over, add " 20", the current user account balance is: 60 
Thread thread C runs over, increase " -80", the current The user account balance is: -20 
Thread thread D runs over, add " -30", the current user account balance is: -50 
Thread thread F runs out, increase " 21", the current user account balance is: -29 
Thread thread E runs End, add " 32", the current user account balance is: 3

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326237851&siteId=291194637