10

I have read some tips that multithread implementation largely depends on the target OS you are working on. And the OS finally provides the multithread capability. Such as Linux has POSIX standard implementation and windows32 has another way.

But I want to know major different in programming language level. C seems to provide more choice for synchronization such as Mutex, read-write locks, record locking, Posix semaphores.

But in Java, I know we can use synchronized works like Mutex? And some other high-level API like AtomicXX and volatile. But I didn't find anything like record locking and read-write locks. Is it a weak side of Java language? Or it is a sacrifice for crossing platform?

Also, I want to know is this a major reason that web server like Nginx and DB like oracle are all written in C/C++?

I am actually a Java developer and I am very curious about it. Hope someone can give me some advice about it.

EDIT:

Paul and Jesper already advised that Java support all the similar lock class like C/C++ after JDK1.5. But if possible, I still wish someone can explain more details why Java provides enough support, we still cannot find a pure Java "oracle".

EDIT:

Also, I want to add something interesting I learned from developer.com by Nasir Khan. Understanding Java Multithreading and Read-Write Locks.

Some topic in it.

  • The interaction of the shared main memory with the thread's local memory,
  • The meaning of "synchronization" with respect to this interaction and mutual exclusion.
  • Clarify the distinction of an object's lock and the resources it guards.

EDIT:

From FileLock JavaDocs

File locks are held on behalf of the entire Java virtual machine. They are not suitable for controlling access to a file by multiple threads within the same virtual machine.

File lock in Java is exactly as same as in C/C++.

UPDATE
I find another interesting area to compare that is
in C++, there is some thing like

atomic<int> x, y;


in JAVA we also have AtomicInteger. Are they the same thing?

Clark Bao
  • 1,743
  • 3
  • 21
  • 39

3 Answers3

6

Java is slightly higher level than C/C++ in most aspects, mainly due to the abstraction that the JVM provides. Thus it is less efficient and further from the OS.

synchronized methods are an example of this, the implementation can use different mechanisms depending on the underlying OS.

Due this lower efficiency C/C++ is preferred for some tasks where efficiency is very important, as the ones you mention.

I would consider that (abstraction due to JVM and thus higher level) as the main reason and source of differences between C/C++ and Java, being how threads are handled and other differences just aspects or consequences of this main difference.

Specifically about read-write locks, Java provides the tools to use them (as pointed in previous comments), and most probably any synchronization method you may want to use is available or implementable in Java in some way. How the JVM translates this to OS calls and the efficiency of the result is a different matter.

jmora
  • 491
  • 3
  • 14
  • "How the JVM translates this to OS calls and the efficiency of the result is a different matter." This is really a good question – Clark Bao Jul 05 '11 at 14:10
  • 1
    That depends on the OS, but I guess developers would take the most straightforward translation available, which would be the simplest to implement and execute. – jmora Jul 05 '11 at 15:32
  • 1
    ~"**less efficient**". How is that measured? – IgorGanapolsky Jun 01 '16 at 18:14
4

Java does provide read-write locks - http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReadWriteLock.html.

Have a look at the java.util.concurrent package if you haven't already. I suspect Java's support is comparable to C's. There are also a number of web servers written in Java that use either multithreaded or async IO (NIO).

Paul Cager
  • 1,910
  • 14
  • 21
  • How about record locking and semaphores I mentioned? – Clark Bao Jul 05 '11 at 13:55
  • Also could you provide some open source project which uses this ReadWriteLock in it? So I can take a look into it. Thanks! – Clark Bao Jul 05 '11 at 14:00
  • 1
    @Clark have a look at the API docs of `java.util.concurrent` as Paul suggests - yes, Java has a `Semaphore` class, and lots of other concurrency control classes as well, including many specialized collection classes. – Jesper Jul 05 '11 at 14:04
  • Yes,there are a number of web servers in JAVA. But how about database? Especially can you name a similiar web server like nginx in its specific domain? – Clark Bao Jul 05 '11 at 14:07
  • 1
    @Clark: There _are_ database systems written in Java (Apache Derby, HSQLDB etc), although they tend _not_ to be the market leaders. For record locking do you mean the sort of file locking provided by http://download.oracle.com/javase/6/docs/api/java/nio/channels/FileLock.html ? – Paul Cager Jul 05 '11 at 14:16
  • @Paul As far as I know H2 do not provide parallel capability. Not sure about Derby. – Clark Bao Jul 05 '11 at 14:19
  • @Paul You are great! I should thank you and accept your answer! But why FileLock is not in concurrent package.A bit Strange. – Clark Bao Jul 05 '11 at 14:22
  • @Clark Bao File locks are for the record locking you asked about. They were added with java.nio.channels in JDK 1.4. java.util.concurrent was added in 1.5. – user207421 Jul 06 '11 at 01:28
1

I believe Java has the lock you mentioned.

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/locks/package-summary.html

And I recommend the book Java Concurrency in Practice to you if you're interested in this topic.

RollingBoy
  • 2,767
  • 1
  • 14
  • 8