简体   繁体   English

了解Java中的同步方法示例

[英]Understanding Synchronized method example in Java

I was reading this link . 我正在阅读此链接。 http://tutorials.jenkov.com/java-concurrency/synchronized.html . http://tutorials.jenkov.com/java-concurrency/synchronized.html

The example is : 例子是:

public class Counter{

  long count = 0;

   public synchronized void add(long value){
     this.count += value;
     System.out.println("Thread="+Thread.currentThread().getId()+"value ="+count);
   }
}

public class CounterThread extends Thread{

 protected Counter counter = null;

 public CounterThread(Counter counter){
    this.counter = counter;
 }

 public void run() {
  for(int i=0; i<10; i++){
       counter.add(i);
    }
 }
}


public class Jenkov2 {

public static void main(String[] args){
  Counter counter = new Counter();
  Thread  threadA = new CounterThread(counter);
  Thread  threadB = new CounterThread(counter);


  threadA.start();
  threadB.start();      
}
 }

When in run this on my system ,it always gives fixed output. 在我的系统上运行此命令时,它始终会提供固定的输出。

    Thread=9value =0
    Thread=9value =1
    Thread=9value =3
    Thread=9value =6
    Thread=9value =10
    Thread=9value =15
    Thread=9value =21
    Thread=9value =28
    Thread=9value =36
    Thread=9value =45
    Thread=10value =45
    Thread=10value =46
    Thread=10value =48
    Thread=10value =51
    Thread=10value =55
    Thread=10value =60
    Thread=10value =66
    Thread=10value =73
    Thread=10value =81
    Thread=10value =90

I run it multiple times but output is always same. 我多次运行它,但输出始终相同。 I was expecting it to be switching between the two threads, value will be 90 after both the threads are done. 我原以为它会在两个线程之间切换,两个线程完成后的值为90。 As per me it will be like this : thread A run method is called, i is 0, 0 will be add and as method is synchronized,it will return after adding zero,then before next iteration of i inside for loop,context switching may occur and thread B runs and value 0 will be added. 按照我的说法,它将是这样的:线程调用run方法,i为0,将添加0,并且由于方法已同步,它将在添加零后返回,然后在i的下一次迭代之前进行for循环,上下文切换发生并且线程B运行并且将添加值0。

Is my understanding correct and the output can also vary between threads or i am missing some concept. 我的理解是正确的,并且输出在线程之间也可能有所不同,或者我缺少一些概念。

Each time one of your threads goes around its loop, it releases the lock, and then immediately tries to lock it again. 每当您的一个线程绕过它的循环时,它都会释放锁定,然后立即尝试再次锁定它。

Looks like on your system, the locking mechanism has no "fairness" to it, and the thread that just let go of the lock is able to grab it again before the other thread even knows it was released. 看起来在您的系统上,锁定机制没有“公平性”,仅释放锁的线程便可以在另一个线程甚至不知道它被释放之前再次抓住它。

It could behave differently on some other system. 在其他系统上,它的行为可能有所不同。 But then again, you never know. 但是话又说回来,你永远都不知道。

Change your run() method if you want to try what @Zabuza suggested, 如果您想尝试@Zabuza建议的内容,请更改run()方法,

public void run() {
    for(int i=0; i<10; i++){
        counter.add(i);
        Thread.sleep(10);
    }
 }

The sleep call will give the other thread a chance to wake up and grab the lock. 睡眠调用将使另一个线程有机会唤醒并抓住锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM