Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in MultiThread aspect between Java and C/C++

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?

like image 394
Clark Bao Avatar asked Jul 05 '11 13:07

Clark Bao


3 Answers

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.

like image 74
jmora Avatar answered Sep 19 '22 04:09

jmora


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).

like image 27
Paul Cager Avatar answered Sep 20 '22 04:09

Paul Cager


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.

like image 36
RollingBoy Avatar answered Sep 20 '22 04:09

RollingBoy