同步块上的 Java 线程锁 [英] Java Thread lock on synchronized blocks

查看:47
本文介绍了同步块上的 Java 线程锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习带锁的同步块.我想知道这个锁和程序中提供的一些第三方锁的区别.

I am learning synchronized blocks with locks.I want to know the difference between this lock and some third party lock that provided in the program.

public class NewThread extends Thread {
StringBuffer sb;
NewThread(StringBuffer sb){
    this.sb=sb;
}
public void run(){
    synchronized(this.sb){
        for(int i=0;i<1000;i++){
            System.out.print(sb);
               try{
             Thread.sleep(5*60);
        }
        catch(Exception e){}
        }
       char c = this.sb.charAt(0);
       this.sb.setCharAt(0, ++c);
    }
}
public static void main(String[] args){
    StringBuffer sb=new StringBuffer("A");
    NewThread nt=new NewThread(sb);
    NewThread nt1=new NewThread(sb);
    NewThread nt2=new NewThread(sb);
    nt.start();
    nt1.start();
    nt2.start();
}

}

如果我要放

public void run(){
    synchronized(this){
        for(int i=0;i<1000;i++){
            System.out.print(sb);
               try{
             Thread.sleep(5*60);
        }
        catch(Exception e){}
        }
       char c = this.sb.charAt(0);
       this.sb.setCharAt(0, ++c);
    }
}

在上面的运行方法中,我在同步块中给出了这个......我想要它们之间的区别

here in the above run method i gave this in the synchronized block ...i want the difference between them

我还有一个问题,如果我们在同步块中提供一个锁对象,而我们没有在该块内使用该对象,那么与普通块相比,我们是否观察到任何特定的东西

i am having one more question ,if we are giving a lock object in synchronized block and we are not using that object inside that block then does we observe any thing specific compared to normal block

推荐答案

如果您有通用 OS API 的并发开发经验,例如Linux中的pthread库,你可能知道我们应该使用锁或其他数据结构来同步访问临界区(共享对象可能被修改)的进程/线程.

If you have concurrent development experience with general OS API, e.g. pthread library in Linux, you may have a knowledge that we should use lock or other data structure to synchronize processes/threads accessing critical section(where shared object may be modified).

Java 使用锁来实现同步块.同步块是一种封装与锁相关的繁琐操作的机制(在操作系统中称为监视器).java中每个对象都有一个锁.同步时,首先锁定共享对象中的锁(也可以说其他进程需要共享对象的锁).如果某个线程获取锁失败,则意味着其他线程正在持有该锁,它必须等待其他线程释放该锁并重新获取,直到它持有该锁,然后进入临界区.

Java use lock to implement synchronization block. Synchronization block is a kind of mechanism(called monitor in operating system) encapsulating tedious operation related to lock. Each object has a lock in java. When synchronized, lock in shared object is locked at first(also we can say other process require lock of the shared object). If some thread fail to acquire the lock, that means some other thread is now holding the lock, it must wait until other thread release the lock and re-acquire again until it hold the lock and then enter the critical section.

第一个代码片段在StringBuffer的实例中使用lock,即sb,每个线程在运行代码之前都会尝试获取sb的锁(调用lock_of_sb.lock().只有成功获取sb的锁才能最终执行代码.

The first code snippet use lock in instance of StringBuffer, i.e. sb, each thread will try to get sb's lock(invoke lock_of_sb.lock() before running the code. Only that acquired sb's lock successfully can eventually execute the code.

至于第二个代码,相当于

As to the second code, which is equivalent to

public synchronized void run(){
  for(int i=0;i<1000;i++){
      System.out.print(sb);
         try{
       Thread.sleep(5*60);
  }
  catch(Exception e){}
  }
  char c = this.sb.charAt(0);
  this.sb.setCharAt(0, ++c);
}

我不认为它的行为符合您的预期.它获取此对象的锁,但是,此对象永远不会共享.所以共享的 sb 在没有任何同步的情况下暴露在临界区中.

I don't think it behaves as you expected. It acquire lock in this object, however, this object is never shared. So the shared sb is exposed into the critical section without any synchronization.

该链接将为您提供另一种实现同步的工作方式.如何同步java中的静态方法 虽然问题本身是关于静态方法的,但它也适用于实例成员方法.

The link will give you another working way to achieve synchronization.How to synchronize static method in java Although the question itself is about static method, it also works well to instance member method.

这篇关于同步块上的 Java 线程锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆