Java关键字synchronized的使用,androidapp开发从入门到精通

private void synMethod() {

synchronized (object){

for (int k = 0; k < 1000; k++) {

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

}

可以发现我们修改了synchronized的位置,但是无一例外,上面的几种写法,都能保证我们实现同步得到正确的值。

上面几种写法唯一不同的是,我们的synchronized关键字修饰的东西不一样。我们的synchronized可以修饰方法,代码块。

具体可以细分位以下几种场景:

1.synchronized 类的实例方法;

2.synchronized 类的静态方法;

3.synchronized 类的class对象;

4.synchronized this(当前类的实例);

5.synchronized obj(任意的实例对象);

第一种场景 synchronized 关键字锁住的是当前类的实例对象。

第二种场景 synchronized 关键字锁住的当前的类对象。

第三种场景 synchronized 关键字锁住的还是当前的类对象。

第四种场景 锁住的当前类的实例。

第五种场景 锁住的任意的实例对象。

注意如果锁主的是类对象的话,尽管new出多个实例,但他们仍然属于同一个类依然会被锁住。

以上五种场景我们主要分为三种情况去讨论:

1锁住当前类的实例,2锁住当前类的类对象,3锁住其他对象的实例

这三种情况有什么区别呢?

下面我们来看代码:

场景1:

Thread thread1 = new Thread(new Runnable() {

@Override

public void run() {

synMethod();

}

},“thread1”);

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

synMethod2();

}

},“thread2”);

thread1.start();

thread2.start();

try {

thread1.join();

thread2.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

Log.v(“MyThreadText”,count+"");

private void synMethod() {

synchronized (this){

for (int k = 0; k < 1000; k++)
{

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

}

private synchronized void synMethod2() {

for (int k = 0; k < 1000; k++) {

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

在这里插入图片描述

场景2:

Thread thread1 = new Thread(new Runnable() {

@Override

public void run() {

synMethod();

}

},“thread1”);

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

synMethod2();

}

},“thread2”);

Thread thread3 = new Thread(new Runnable() {

@Override

public void run() {

synMethod3();

}

},“thread3”);

thread1.start();

thread2.start();

thread3.start();

try {

thread1.join();

thread2.join();

thread3.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

Log.v(“MyThreadText”,count+"");

private void synMethod() {

synchronized (object){

for (int k = 0; k < 5; k++) {

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

}

private synchronized void synMethod2() {

for (int k = 0; k < 5; k++) {

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

public void synMethod3() {

synchronized (SynchronizedActivity.class){

for (int k = 0; k < 5; k++) {

count++;

Log.v(“MyThreadText”,Thread.currentThread().getName()+" k:" +k + " count:"+count);

}

}

}

在这里插入图片描述

场景3:

Thread thread1 = new Thread(new Runnable() {

@Override

public void run() {

synMethod();

}

},“thread1”);

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

synMethod3();

}

},“thread2”);

thread1.start();

thread2.start();

try {

thread1.join();

thread2.join();

} catch (InterruptedException e) {

() {

@Override

public void run() {

synMethod();

}

},“thread1”);

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

synMethod3();

}

},“thread2”);

thread1.start();

thread2.start();

try {

thread1.join();

thread2.join();

} catch (InterruptedException e) {

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值