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.
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?
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With