English 中文(简体)
贾瓦同步的关键词——它是否保护一种同级方法不会同时执行?
原标题:Java synchronized keyword - Does it protect a class method from being executed at the same time?
  • 时间:2011-08-01 12:36:26
  •  标签:
  • java

我需要一些澄清语气,说明一下 s6号的同化关键词是如何运作的。

采用以下分类。

class Carrier {

   private String[] _collection = new String[2];

   public Carrier() {
       this._collection[0] = "abc";
       this._collection[1] = "123";
   }

   public syncronized void change(int cId) {
       Thread.sleep(3000);
       this._collection[cId] = "changed";
   }

}

现在,申请中有些地方提到同一承运人类别标的,因此,可能同时使用(改动)方法。

......carrier.change(1);

......

......carrier.change(1);

合并的关键词是否会妨碍对方法的同化执行? 它是否只是要发出呼吁,等待每个人完成。

最佳回答

是的,这将防止这两种方法同时执行。

这确实是<条码>同步化<条码>的主要用途。 关键词是。

当一种同步方法运行时,该方法在所呼吁的物体上获得了锁。 这意味着,在这种方法锁定时,不能对其他同步编码进行操作。 一旦执行该方法,该方法将释放锁,其他同步方法也可在该物体上获得锁。

问题回答

Yes, the second thread will block while the first thread is executing. That s because they both try to acquire the monitor on the same object - via this, in this case. So your change method is equivalent to:

public void change(int cId) {
    synchronized (this) {
        Thread.sleep(3000);
        this._collection[cId] = "changed";
    }
}

Personally I don t like synchronizing on "this" as it means any other code with access to the object itself could acquire the same monitor. I prefer to create an object just for locking which only code within the class has access to:

private final Object lock = new Object();

public void change(int cId) {
    synchronized (lock) {
        Thread.sleep(3000);
        this._collection[cId] = "changed";
    }
}

That will still have the same effect in terms of two threads calling change on the same object, because both threads will still be acquiring the same monitor - just the one associated with the lock-specific object.

Yes, and yes. (Note that it s synchronized, not syncronized)

Yes - the synchronized keyword prevents simultaneous execution of the method by different threads.

Note: A "class method" is a static method. An "instance method" (ie non-static) is what you have in your question.

synchronized are used for preventing simultaneously accesses in cuncurrent programming. For example when you have to client, one that reads and another that writes:

Lock here for an exhaustive explanation.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签