问题描述
因此我使用 synchronized
关键字进行测试。这是我尝试的一个例子:
so i was testing with synchronized
keyword. Here is an example that I tried:
public class MyTest {
static int i = 0;
public static void main(String[] args) {
new Thread(t1).start();
new Thread(t2).start();
}
private static void countMe(String name){
i++;
System.out.println("Current Counter is: " + i + ", updated by: " + name);
}
private static Runnable t1 = new Runnable() {
public void run() {
try{
for(int i=0; i<5; i++){
countMe("t1");
}
} catch (Exception e){}
}
};
private static Runnable t2 = new Runnable() {
public void run() {
try{
for(int i=0; i<5; i++){
countMe("t2");
}
} catch (Exception e){}
}
};
}
当我运行它时,调用 countMe的输出()
来自两个线程的方法生成此输出:
When I run it, the output of calling countMe()
method from two threads generates this output:
Current Counter is: 1
Current Counter is: 2
Current Counter is: 4
Current Counter is: 5
Current Counter is: 6
Current Counter is: 7
Current Counter is: 3
Current Counter is: 8
Current Counter is: 9
Current Counter is: 10
当我将方法 countMe()
更改为:
private synchronized static void countMe(){
i++;
System.out.println("Current Counter is: " + i);
}
我得到这个输出:
Current Counter is: 1
Current Counter is: 2
Current Counter is: 3
Current Counter is: 4
Current Counter is: 5
Current Counter is: 6
Current Counter is: 7
Current Counter is: 8
Current Counter is: 9
Current Counter is: 10
虽然这让我清楚地了解了 synchronized
,我想知道还有其他原因,我们可以使用 synchronized
。或者我在这里做了什么,是我们需要使用这个 synchronized
关键字的唯一原因?
Although this gives me clear understanding the purpose of synchronized
, I want to know is there any other reason as well, that we can use synchronized
. Or what I have done here, is the only eason why we need the use of this synchronized
keyword?
谢谢。
编辑:我感到困惑的另一件事是,在第一次输出时为什么计数器在7之后变为3.这似乎有点不可能对我而言,每次尝试都会发生类似的结果,这是正常的吗?
Another thing that I am confused with is that in first output why the counter went to 3 after 7. It seems a bit impossible to me, but similar results do happen every time I try, is this normal?
推荐答案
两件事:
首先,对同一对象的两次同步方法的调用不可能进行交错。当一个线程正在为一个对象执行一个synchronized方法时,所有其他线程都会调用同一个对象的同步方法阻塞(暂停执行),直到第一个线程完成该对象为止。
First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
其次,当synchronized方法退出时,它会自动与同一对象的同步方法的任何后续调用建立before-before关系。这可以保证对所有线程都可以看到对象状态的更改。
Second, when a synchronized method exits, it automatically establishes a happens-before relationship with any subsequent invocation of a synchronized method for the same object. This guarantees that changes to the state of the object are visible to all threads.
同步方法启用了一个简单的策略来防止线程干扰和内存一致性错误:如果一个对象是对多个线程可见,对该对象变量的所有读取或写入都是通过同步方法完成的。 (一个重要的例外:构造对象后无法修改的final字段,一旦构造了对象,就可以通过非同步方法安全地读取)。
Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. (An important exception: final fields, which cannot be modified after the object is constructed, can be safely read through non-synchronized methods, once the object is constructed).
来源:
这篇关于学习Java,使用synchronized关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!