Thread synchronization
A means to ensure secure access to multiple threads compete for resources, avoid multiple threads simultaneously modify a resource, leading to inaccuracies resources.
Related concepts :
- What is the competition for resources
- When synchronization is required
- How to synchronize
- The competition marked as private resources
- The method involves the static resource block or modify the keywords with synchrized
Synchronization method
Bank transfer function simulation
package cn.domin.threadr.sync;
public class Thransfer {
User u = new User();
u.setBalance(10000);
u.setCode(0x08);
ATMThread a1 = new ATMThread("ATM1", u, 1000);
ATMThread a2 = new ATMThread("ATM2", u, 5000);
ATMThread a3 = new ATMThread("ATM3", u, 3000);
a1.start();
a2.start();
a3.start();
}
}
class ATMThread extends Thread{
private User u;
private int y;
public ATMThread(String name,User u,int y) {
super(name);
this.u = u;
this.y = y;
}
public void oper(int y) {
int m = u.oper(y);
System.out.println(Thread.currentThread().getName()+"操作"+y+"元,账户余额"+m+"元");
}
public void run() {
oper(y);
}
}
class User{
private int code;
private int balance;
public synchronized int oper(int y) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
balance += y;
return balance;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
}
volitile keyword
- Modified variables can only be operated at the same time by a thread
- Note that, when a value of the variable value and its own independent, the operation is made in the atomic level
- Atomic operation, means will not be interrupted thread scheduling operation, the thread
- volatile attribute operations does not guarantee atomicity operations may be used synchronized keyword, locking mechanism, to ensure synchronization type atomic operation.
public class Thransfer {
private volatile int it;
public static void main(String[] args) {
User u = new User();
u.setBalance(10000);
u.setCode(0x08);
// ATMThread a1 = new ATMThread("ATM1", u, 1000);
// ATMThread a2 = new ATMThread("ATM2", u, 5000);
// ATMThread a3 = new ATMThread("ATM3", u, 3000);
// a1.start();
// a2.start();
// a3.start();
final Thransfer t = new Thransfer();
for (int i = 0; i < 100; i++) {
new Thread(new Runnable() {
@Override
public void run() {
t.cre();
}
}).start();
}
while(Thread.activeCount()>1) {
Thread.yield();
}
System.out.println(t.it);
}
public synchronized void cre() {
this.it++;
}
}
The concept of knowledge
Java Memory Model:
- Visibility
modify the state between threads are visible. That one thread modifies a result, another thread is immediately visible. For example volitile keyword, its modified variables are not allowed thread cache and reorder, so the other threads are visible, pay attention to indivisibility. - Atom of the
atom is the smallest unit in the world, has the inalienable nature, then we call indivisible operation is an atomic operation, n ++ and n = n + 1 do not belong to an atomic operation, because he is separated. - Ordering
java provides two ways to ensure the orderly operation of the thread between xi
- volitile semantic prohibition instruction itself remake
- synchronization keywords at the same time allowing only one thread to operate, is a serial operation, to ensure orderly between threads.