关于Java中synchronized关键字的案例

synchronize同步问题对于锁得理解

package test.synchronize;

public class AmSynchornizeClass {
private int index=0;

private static int staticIndex = 0;

//成员锁对象
private Object lock = new Object();


/**
* 锁对象是this
* @return
*/
public synchronized int getIndex() {
return index;
}

/**
* 锁对象this 等同于 public synchronized int getIndex(){...}
*/
public int getIndex2() {
synchronized (this) {
return index;
}
}

/**
* 锁对象时成员变量lock
* @return
*/
public int getIndex3() {
synchronized (lock) {
return index;
}
}

/**
* 错误案例:锁对象new Object()离开了方法getIndex4()块没有实际意义
* @return
*/
public int getIndex4(){
synchronized(new Object()){
return index;
}
}

public synchronized void increaseIndex() {
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
index++;
}

public void increaseIndex2() {
synchronized(this){
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
index++;
}
}

public void increaseIndex3() {
synchronized(lock){
try {
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
index++;
}
}

/**
* 锁对象是AmSynchornizeClass.class等同于public static void substract2()
*/
public synchronized static void substract(){
staticIndex--;
}

public static void substract2(){
synchronized(AmSynchornizeClass.class){
staticIndex--;
}
}
}



package test.synchronize;

import org.junit.Test;

public class TestSynchronize {
@Test
public void testSynchronizedLock1() {
final AmSynchornizeClass asc1 = new AmSynchornizeClass();
final AmSynchornizeClass asc2 = new AmSynchornizeClass();
Runnable getThread = new Runnable(){
@Override
public void run() {
System.out.println("go into get method()...");
System.out.println(asc1.getIndex());
System.out.println("get out from get method()...");
}
};

Runnable increaseThread = new Runnable(){
@Override
public void run() {
System.out.println("go into increase method()...");
asc2.increaseIndex();
System.out.println("go out increase method()...");
}
};

Thread t1= new Thread(increaseThread);
Thread t2= new Thread(getThread);

t1.start();
t2.start();
try {
while(true){
if(t1.isAlive() || t2.isAlive()){
Thread.sleep(200);
} else {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

@Test
public void testSynchronizeLock() {
final AmSynchornizeClass asc = new AmSynchornizeClass();
Runnable getThread = new Runnable(){
@Override
public void run() {
System.out.println("go into get method()...");
System.out.println(asc.getIndex());
System.out.println("get out from get method()...");
}
};

Runnable increaseThread = new Runnable(){
@Override
public void run() {
System.out.println("go into increase method()...");
asc.increaseIndex();
System.out.println("go out increase method()...");
}
};

Thread t1= new Thread(increaseThread);
Thread t2= new Thread(getThread);

t1.start();
t2.start();
try {
while(true){
if(t1.isAlive() || t2.isAlive()){
Thread.sleep(200);
} else {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值