线程周期
线程同步
synchronized 同步方法
Java中的每一个对象都有一个锁(lock)或者叫做监视器(monitor), 当访问某一个对象的synchronized方法时,表示该对象上锁,此时其他任何线程都无法再去访问该synchronized方法了,直到之前的那个线程执行方法完毕后(或者是抛出了异常),那么该对象的锁释放掉,其他线程才有可能再去访问该synchronized方法。
如果一个对象有多个synchronized方法,某一时刻某个线程已经进入到了某个synchronized方法,那么在该方法没有执行完毕前,其他线程是无法访问该对象的任何synchronized方法的。
如果某个synchronized方法是static额,那么当线程访问该方法时,它锁的并不是synchronized方法所在的对象,而是synchronized方法所在的对象所对应的Class对象,因为Java中无论一个类有多少个对象,这些对象会对应唯一的一个Class对象,一次当线程分别访问同一个类的两个对象的的两个static,synchronized方法是,他们的执行顺序也是顺序的,也就是说一个线程先去执行方法,执行完毕后另一个线程才开始执行。
package org.zbq.thread;
class Bank {
private int money = 1000;
public synchronized int getMoney(int number){
if(number < 0){
return -1;
}else if(number > money){
return -2;
}else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
money -= number;
System.out.println("Left money:" + money);
return number;
}
}
}
class MoneyThread extends Thread {
private Bank bank;
public MoneyThread(Bank bank) {
this.bank = bank;
}
@Override
public void run() {
System.out.println(bank.getMoney(800));
}
}
public class FetchMoney {
public static void main(String[] args) {
Bank bank = new Bank();
Thread t1 = new MoneyThread(bank);
Thread t2 = new MoneyThread(bank);
t1.start();
t2.start();
}
}
加了synchronized方法
Left money:200
-2
800
不加synchronized方法
Left money:-600
800
Left money:-600
800
synchronized 同步代码块
class Bank {
private int money = 1000;
public int getMoney(int number){
synchronized(this) {
if(number < 0){
return -1;
}else if(number > money){
return -2;
}else {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
money -= number;
System.out.println("Left money:" + money);
return number;
}
}
}
}
synchronized 方法是一种粗粒度的并发控制,某一时刻只能有一个线程执行该synchronized方法, synchronized块则是一种细粒度的并发控制,只会将块中的代码同步,位于方法内,synchronized块之外的代码可以被多个线程同时访问到。
Wait 和 notify
package org.zbq.thread;
class Sample {
private int number;
public synchronized void increase() {
if(0 != number) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number++;
System.out.println(number);
notify();
}
public synchronized void decrease() {
if(0 == number) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
number--;
System.out.println(number);
notify();
}
}
class IncreaseThread extends Thread {
private Sample sample;
public IncreaseThread(Sample sample) {
this.sample = sample;
}
@Override
public void run() {
for(int i=0; i<5; i++) {
try {
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sample.increase();
}
}
}
class DecreaseThread extends Thread {
private Sample sample;
public DecreaseThread(Sample sample) {
this.sample = sample;
}
@Override
public void run() {
for(int i=0; i<5; i++) {
try {
Thread.sleep((long)(Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
this.sample.decrease();
}
}
}
public class Wait {
public static void main(String[] args) {
Sample sample = new Sample();
Thread th1 = new IncreaseThread(sample);
Thread th2 = new DecreaseThread(sample);
th1.start();
th2.start();
}
}
输出结果
1
0
1
0
1
0
1
0
1
0
这两个方法要求在调用时线程应该已经获得了对象的锁,因此对这两个方法的调用需要放在synchronized方法或块中。当线程执行了wait方法时,它会释放掉对象的锁。
另一个导致线程暂停的方法就是Thread类的sleep方法,它会导致线程睡眠指定的毫秒数,但线程在睡眠的过程中不会释放掉对象的锁。