简体   繁体   English

Java:如何创建同步块及其子类使用

[英]Java: How to create synchronized block and that subclasses use

abstract class A {
  protected abstract int isRunning();

  public void concreteMethod() {
    synchroinzed(isRunning()) {
       //do stuff
    }
  }
}

class B extends A {
  int running_ = 0;

  public int isRunning() {
     return running_;
  }
}

The problem I get: int is not a valid type's argument for the synchronized statement. 我得到的问题是:int对于同步语句不是有效类型的参数。 How do I do this? 我该怎么做呢?

If you want to prevent simultaneous execution of your block and the method isRunning() , you can't do it exactly as you want because synchronization can't be inherited (only an implementation can justify synchronization ). 如果你想防止您的块的同时执行和方法isRunning()你不能这样做正是你想要的,因为synchronization不能被继承(仅实现可以证明synchronization )。

Here's the nearest you can do : 这是您可以做的最近的事情:

class A {
  protected Object lock = new Object();
  protected abstract int isRunning();
  public void concreteMethod() {
    synchronized(lock) {
       //do stuff
    }
  }
}

class B extends A {
  int running_ = 0;    
  public int isRunning() {
    synchronized(lock) {
     return running_;
    }
  }
}

If you can afford to lock the entire concreteMethod() and not just a block, you have the simple solution to add the synchronized keyword to the concreteMethod and isRunning() : 如果你能负担得起锁定整个concreteMethod()而不是仅一个街区,你有简单的解决增加synchronized关键字的concreteMethodisRunning()

class A {
  protected abstract int isRunning();
  public synchronized void concreteMethod() {
       //do stuff
  }
}

class B extends A {
  int running_ = 0;    
  public synchronized int isRunning() {
     return running_;
  }
}

You cannot have a lock on primitive value ( int in your case since isRunning() returns an int ). 您不能锁定原始值(在本例中为int ,因为isRunning()返回int )。

Try to replace int by Integer for instance. 例如,尝试将int替换为Integer

Here is what the official documentation say 这是官方文件说的

Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock

Meaning, for synchronization you would first need an object to lock on. 意思是,对于同步,您首先需要一个对象才能锁定。 Since int is not an object you get an error. 由于int不是对象,因此会出现错误。 An Integer object would not give this error but just using an Integer instead of int would not guarantee correctness. 一个Integer对象不会给出此错误,但是仅使用Integer而不是int不能保证正确性。

Here is a very good tutorial on java concurrency 这是一个关于Java并发性的很好的教程

http://tutorials.jenkov.com/java-concurrency/index.html http://tutorials.jenkov.com/java-concurrency/index.html

I think you are confusing two concepts. 我认为您混淆了两个概念。 Synchronized block always asks for reference as its parameter time but you are passing int (primitive data type ) as its argument. 同步块总是要求引用作为其参数时间,但是您正在传递int(原始数据类型)作为其参数。 You can try using 'this' keyword (in place of isRunning() method) as synchronized block parameter. 您可以尝试使用'this'关键字(代替isRunning()方法)作为同步块参数。 Also you need to declare class A as abstract. 另外,您需要将A类声明为抽象。 Also there is a spelling mistake in your code. 您的代码中也存在拼写错误。 You can also consider @dystroy 's solution. 您还可以考虑@dystroy的解决方案。

I guess you are looking for this: 我想您正在寻找:

class A
{
  protected abstract int isRunning();

  public synchronized void concreteMethod()
  {
       //do stuff
  }
}

class B extends A
{
  int running_ = 0;

  public synchronized int isRunning()
  {
   return running_;
  }

}

- sychronized block is accompanied by an Object whose lock is needed to be obtained to access that block by the thread of execution. - sychronized块附带一个Object ,该Object 的执行线程需要获取锁才能访问该块。

- int is a primitive type and not an object. - int是一个primitive类型,而不是一个对象。

- Creating an sychronized block along with an Object of the Super-Class is well accessible by its Sub-Classes. -通过其子类很好地访问与超类对象一起创建同步块。

Eg: 例如:

public void concreteMethod()
  {
    synchroinzed(A.class) // or synchroinzed(A.class)
    {
       //do stuff
    }
  }

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

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