Java语言的关键字,可用来给对象和方法或者代码块加锁,当它锁定一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码。当两个并发线程访问同一个对象object中的这个加锁同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。然而,当一个线程访问object的一个加锁代码块时,另一个线程仍然可以访问该object中的非加锁代码块。
Note:
当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞
1.方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之前.这时,线程获得的是成员锁,即一次只能有一个线程进入该方法,其他线程要想在此时调用该方法,只能排队等候,当前线程(就是在synchronized方法内部的线程)执行完该方法后,别的线程才能进入.
例如:
public synchronized void synMethod(){
//方法体
}
如在线程t1中有语句obj.synMethod(); 那么由于synMethod被synchronized修饰,在执行该语句前, 需要先获得调用者obj的对象锁, 如果其他线程(如t2)已经锁定了obj (可能是通过obj.synMethod,也可能是通过其他被synchronized修饰的方法obj.otherSynMethod锁定的obj), t1需要等待直到其他线程(t2)释放obj, 然后t1锁定obj, 执行synMethod方法. 返回之前之前释放obj锁.
2.对某一代码块使用,synchronized后跟括号,括号里是变量,这样,一次只有一个线程进入该代码块.此时,线程获得的是成员锁
public class SynchronizedField {
private static Double balance = 100.0;
public static void main(String[] args) {
lockField();
}
public static void lockField() {
Thread withDrawThread = new WithdrawThread(10.0);
Thread depositThread = new Thread(new DepositThread(10.0));
withDrawThread.start();
depositThread.start();
}
private static final class WithdrawThread extends Thread {
private Double amount;
public WithdrawThread(Double amount) {
this.amount = amount;
}
public void run() {
synchronized (balance) {
System.out.println("Before withdrawing " + amount + ", the balance is " + balance);
balance -= amount;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Withdrew " + amount + ", the balance is " + balance);
}
}
}
private static final class DepositThread implements Runnable {
private Double amount;
public DepositThread(Double amount) {
this.amount = amount;
}
public void run() {
synchronized (balance) {
System.out.println("Before depositing " + amount + ", the balance is " + balance);
balance += amount;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Deposited " + amount + ", the balance is " + balance);
}
}
}
}
public class SynchronizedMethod {
private static Double balance = 100.0;
public static void main(String[] args) {
lockMethod();
}
public static void lockMethod() {
Thread depositThread1 = new Thread(new DepositThread(10.0));
Thread depositThread2 = new Thread(new DepositThread(10.0));
depositThread1.start();
depositThread2.start();
}
private static final class DepositThread implements Runnable {
private Double amount;
public DepositThread(Double amount) {
this.amount = amount;
}
public void run() {
deposit(amount);
}
}
public static synchronized void deposit(Double amount) {
System.out.println("Before depositing " + amount + ", the balance is " + balance);
balance += amount;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Deposited " + amount + ", the balance is " + balance);
}
}
Notes:
deposit方法不能写在DepositThread里
class lock和object lock
public class LearnVolatile {
public synchronized void SyncForInstance() {
for(int i=0; i<10; i++) {
System.out.println("SyncForInstance");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static synchronized void SyncForClass() {
for(int i=0; i<10; i++) {
System.out.println("SyncForClass");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static Thread t1 = new Thread() {
public void run() {
SyncForClass();
}
};
static Thread t2 = new Thread() {
public void run() {
new LearnVolatile().SyncForInstance();
}
};
public static void main(String[] args) {
t1.start();
t2.start();
}
}
结果如下:
SyncForClass
SyncForInstance
SyncForClass
SyncForInstance
……
public class LearnVolatile {
private static Lock lock = new ReentrantLock();
public void SyncForInstance() {
try {
lock.lock();
for(int i=0; i<10; i++) {
System.out.println("SyncForInstance");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}
public static void SyncForClass() {
try {
lock.lock();
for(int i=0; i<10; i++) {
System.out.println("SyncForClass");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
lock.unlock();
}
}
static Thread t1 = new Thread() {
public void run() {
SyncForClass();
}
};
static Thread t2 = new Thread() {
public void run() {
new LearnVolatile().SyncForInstance();
}
};
public static void main(String[] args) {
t1.start();
t2.start();
}
}
结果如下:
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForClass
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
SyncForInstance
Exception 1
public class LearnThread {
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
System.out.println("....");
}
};
t1.start();
t1.start();
}
}
....
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Thread.java:704)
at com.jesse.core.learnvolatile.LearnThread.main(LearnThread.java:12)
Reason:线程的生命周期(dead)
①NEW:这种情况指的是,通过New关键字创建了Thread类(或其子类)的对象
②RUNNABLE:这种情况指的是Thread类的对象调用了start()方法,这时的线程就等待时间片轮转到自己这,以便获得CPU;第二种情况是线程在处于RUNNABLE状态时并没有运行完自己的run方法,时间片用完之后回到RUNNABLE状态;还有种情况就是处于BLOCKED状态的线程结束了当前的BLOCKED状态之后重新回到RUNNABLE状态。
③RUNNING:这时的线程指的是获得CPU的RUNNABLE线程,RUNNING状态是所有线程都希望获得的状态。
④DEAD:处于RUNNING状态的线程,在执行完run方法之后,就变成了DEAD状态了。
⑤BLOCKED:这种状态指的是处于RUNNING状态的线程,出于某种原因,比如调用了sleep方法、等待用户输入等而让出当前的CPU给其他的线程。
简单的死锁
public class DeadLock {
private static Lock lockA = new ReentrantLock();
private static Lock lockB = new ReentrantLock();
public static void main(String[] args) {
Thread t1 = new Thread() {
public void run() {
try {
lockA.lock();
System.out.println("t1 get lockA");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t1 is waiting lockB");
lockB.lock();
} finally {
lockA.unlock();
lockB.unlock();
}
}
};
Thread t2 = new Thread() {
public void run() {
try {
lockB.lock();
System.out.println("t2 got lockB");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("t2 is waiting for lockA");
lockA.lock();
} finally {
lockB.unlock();
lockA.unlock();
}
}
};
t1.start();
t2.start();
}
}
t2 got lockB
t1 get lockA
t2 is waiting for lockA
t1 is waiting for lockB