Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of C# lock in Java?

Equivalent of C# lock in Java?

For example:

public int Next() {
  lock (this) {
    return NextUnsynchronized();
  }
}

How do I port this C# method to Java?

like image 444
user3111311 Avatar asked Mar 24 '14 11:03

user3111311


People also ask

What is equivalent to classes in C?

The closest thing you can get is a struct .

Is there pass in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

Is there classes in C?

1. C Classes. A class consists of an instance type and a class object: An instance type is a struct containing variable members called instance variables and function members called instance methods.

Are templates supported in C?

The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.


1 Answers

public int Next() {
    synchronized (this) {
        return NextUnsynchronized();
    }
}

That's it. And the next code is better.

public synchronized int Next() {
    return NextUnsynchronized();
}
like image 109
ikh Avatar answered Oct 04 '22 08:10

ikh