Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码。
一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。
结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
五、以上规则对其它对象锁同样适用。
举例说明:
一、当两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。
public class mySyschronized implements Runnable{
@Override
public void run() {
synchronized (this) {
for(int i=0;i<5;i++){
System.out.println(Thread.currentThread().getName() + "当前线程:" + i);
}
}
}
public static void main(String[] args) {
mySyschronized myThread1 = new mySyschronized();
new Thread(myThread1, "A").start();
new Thread(myThread1, "B").start();
}
}
输出结果为:
A当前线程:0
A当前线程:1
A当前线程:2
A当前线程:3
A当前线程:4
B当前线程:0
B当前线程:1
B当前线程:2
B当前线程:3
B当前线程:4
二、然而,当一个线程访问object的一个synchronized(this)同步代码块时,另一个线程仍然可以访问该object中的非synchronized(this)同步代码块。
public class mySynchioned02 {
/**
* 线程同步块
* */
public void getThread(){
synchronized (this) {
int i = 5;
while(i-- > 0){
try{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "线程同步块: " + i);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 非线程同步块
* */
public void getNotThread(){
int i = 5;
while(i-- > 0){
try{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "非线程同步块:" + i);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* myThread类虽然没有继承Thread实现Runnable,但在main方法里面new Thread(new Runnable).start().
* 其效果与继承Thread实现Runnable一样的情况,在线程里面我们提倡使用实现Runnable接口,有利于程序的扩展。
* Java是单继承,多可实现
* */
public static void main(String[] args) {
final mySynchioned02 myThread = new mySynchioned02();
//调用线程同步块
new Thread(new Runnable() {
@Override
public void run() {
//匿名类访问外部方法或实例,外部方法或实例需要用final修饰
myThread.getThread();
}
},"Y").start();
//调用非线程同步块
new Thread(new Runnable() {
@Override
public void run() {
//匿名类访问外部方法或实例,外部方法或实例需要final修饰
myThread.getNotThread();
}
},"N").start();
}
}
输出结果:
Y线程同步块: 4
N非线程同步块:4
Y线程同步块: 3
N非线程同步块:3
Y线程同步块: 2
N非线程同步块:2
Y线程同步块: 1
N非线程同步块:1
Y线程同步块: 0
N非线程同步块:0
三、尤其关键的是,当一个线程访问object的一个synchronized(this)同步代码块时,其他线程对object中所有其它synchronized(this)同步代码块的访问将被阻塞。
修改说明二的例子中genNotThread()方法,如下
/**
* 非线程同步块
* */
public void getNotThread(){
synchronized (this) {
int i = 5;
while(i-- > 0){
try{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "非线程同步块:" + i);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
输出结果:
Y线程同步块: 4
Y线程同步块: 3
Y线程同步块: 2
Y线程同步块: 1
Y线程同步块: 0
N非线程同步块:4
N非线程同步块:3
N非线程同步块:2
N非线程同步块:1
N非线程同步块:0
四、第三个例子同样适用其它同步代码块。也就是说,当一个线程访问object的一个synchronized(this)同步代码块时,它就获得了这个object的对象锁。结果,其它线程对该object对象所有同步代码部分的访问都被暂时阻塞。
修改说明二的例子中genNotThread()方法,如下
/**
* 非线程同步块
* */
public synchronized void getNotThread(){
int i = 5;
while(i-- > 0){
try{
Thread.sleep(500);
System.out.println(Thread.currentThread().getName() + "非线程同步块:" + i);
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
输出结果:
Y线程同步块: 4
Y线程同步块: 3
Y线程同步块: 2
Y线程同步块: 1
Y线程同步块: 0
N非线程同步块:4
N非线程同步块:3
N非线程同步块:2
N非线程同步块:1
N非线程同步块:0
五、以上规则对其它对象锁同样适用