First, thread synchronization
Thread Synchronization: that is, when there is a thread in the memory operation, other threads can not operate on this memory address until the thread to complete the operation, other threads to operate the memory address, and other threads and in a wait state there are many ways to achieve thread synchronization, critical section object is one of them.
In multi-threaded programming inside, some of sensitive data is allowed to be accessed by multiple threads at the same time, this time on the use of synchronous access technology to ensure the data at any time, there is at most one thread access, in order to ensure data integrity.
Second, multithreaded synchronization solution
2.1 synchronized block:
Use synchronized () statement is required to complete the implementation of a "package", synchronized (Obj obj) constructor can be passed in any class of objects,
But since it is the listener on a unique object passed to ensure the uniqueness of the "lock", so the general use of the shared resources of the object obj passed as a synchronized (Obj obj) where:
Just need to lock Account class method to get money to save money on the line:
package com.test.threadDemo2; /** * Bank Account * @author Administrator * */ public class Acount { private int count=0; /** * Save money * @param money */ public void addAcount(String name,int money) { synchronized(this) { // 存钱 count += money; System.out.println(name+"...存入:"+money+"..."+Thread.currentThread().getName()); SelectAcount(name); } } /** * Withdrawals * @Param Money * / public void subAcount (String name, int Money) { the synchronized ( the this ) { // first determine the current account balance is enough to withdraw money amount IF (COUNT-Money <0 ) { System.out.println ( "insufficient account balance!" ); Return ; } // get money COUNT - = Money; System.out.println(name+"...取出:"+money+"..."+Thread.currentThread().getName()); SelectAcount(name); } } /** * Check balances */ public void SelectAcount(String name) { System.out.println(name+"...余额:"+count); } }
2.2 synchronous method
Or affirm synchronized to the method's stated:
package com.test.threadDemo2; /** * Bank Account * @author Administrator * */ public class Acount { private int count; /** * Save money * @param money */ public synchronized void addAcount(String name,int money) { // 存钱 count += money; System.out.println(name+"...存入:"+money); } /** * Withdrawals * @Param Money * / public the synchronized void subAcount (String name, int Money) { // first determine the current account balance is enough to withdraw money amount IF (COUNT-Money <0 ) { System.out.println ( "insufficient account balance!" ); Return ; } // get money COUNT - = Money; System.out.println(name+"...取出:"+money); } /** * Check balances */ public void SelectAcount(String name) { System.out.println(name+"...余额:"+count); } }
2.3 synchronous lock:
After the account is created private ReetrantLock class object, call lock () method, synchronous execution body is finished, the need to use the unlock () to release the lock.
package com.test.threadDemo2; import java.util.concurrent.locks.ReentrantLock; /** * Bank Account * @author Administrator * */ public class Acount { private int count; private ReentrantLock lock = new ReentrantLock(); /** * Save money * @param money */ public void addAcount(String name,int money) { lock.lock(); the try { // save COUNT + = Money; System.out.println(name+"...存入:"+money); }finally { lock.unlock(); } } /** * Withdrawals * @param money */ public void subAcount(String name,int money) { lock.lock(); the try { // first determine the current account balance is enough to withdraw money amount IF (COUNT-Money <0 ) { System.out.println ( "insufficient account balance!" ); Return ; } // get money COUNT - = Money; System.out.println(name+"...取出:"+money); }finally { lock.unlock(); } } /** * Check balances */ public void SelectAcount(String name) { System.out.println(name+"...余额:"+count); } }
Third, the thread communication
Increase dart flag in the shared resource, when the dart flag is true when it can save money, save finished put the dart flag is set to false, when withdrawals discovery dart flag is false, we can withdraw, take their money put dart flag set to true.
Simply modify and test the Account class to class:
package com.test.threadDemo2; /** * Bank Account * @author Administrator * * / Public class Acount { Private Boolean flag = to false ; // default flag is false, then the deposit requirement must withdraw Private int COUNT = 0 ; /** * Save money * @Param Money * / public void addAcount (String name, int Money) { the synchronized ( the this ) { // Flag represents deposits is true, otherwise it can not deposit IF (Flag) { the try { the this .wait (); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } The else { // save COUNT + = Money; System.out.println(name+"...存入:"+money+"..."+Thread.currentThread().getName()); SelectAcount(name); flag = true; this.notifyAll(); } } } /** * Withdrawals * @param money */ public void subAcount(String name,int money) { synchronized(this) { if(!flag) { try { this.wait(); } catch (InterruptedException e) { e.printStackTrace (); } } The else { // first determine the current account balance is enough to withdraw money amount IF (COUNT-Money <0 ) { System.out.println ( "insufficient account balance!" ); Return ; } // get money COUNT - = Money; System.out.println(name+"...取出:"+money+"..."+Thread.currentThread().getName()); SelectAcount(name); flag = false; this.notifyAll(); } } } /** * Check balances */ public void SelectAcount(String name) { System.out.println(name+"...余额:"+count); } }
package com.test.threadDemo2; public class ThreadDemo2 { public static void main(String[] args) { // open a bank account Acount acount = new new Acount (); // After the bank to open a bank account to bank cards Card card1 = new new Card ( "card1" , acount); Card card2 = new Card("card2",acount); Card card3 = new Card("card3",acount); // After the bank to open a bank account passbook sheets Paper paper1 = new new Paper ( "paper1" , acount); Paper paper2 = new Paper("paper2",acount); // Create three bank cards the Thread Thread1 = new new the Thread (card1, "card1" ); Thread thread2 = new Thread(card2,"card2"); Thread3 the Thread = new new the Thread (card3, "card3" ); // Create two books the Thread thread4 = new new the Thread (paper1, "paper1" ); Thread thread5 = new Thread(paper2,"paper2"); thread1.start(); thread2.start(); thread3.start(); thread4.start(); thread5.start(); } }