简体   繁体   English

Java 和 C/C++ 在多线程方面的区别

[英]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.比如Linux有POSIX标准实现,windows32有另一种方式。

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. C 似乎提供了更多的同步选择,例如互斥锁、读写锁、记录锁、Posix 信号量。

But in Java, I know we can use synchronized works like Mutex?但是在 Java 中,我知道我们可以使用像 Mutex 这样的同步作品? And some other high-level API like AtomicXX and volatile.以及其他一些高级 API,例如 AtomicXX 和 volatile。 But I didn't find anything like record locking and read-write locks.但是我没有找到像记录锁和读写锁这样的东西。 Is it a weak side of Java language?它是 Java 语言的弱点吗? 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++?另外,我想知道这是 web 服务器(如 Nginx)和 DB(如 oracle)都是用 C/C++ 编写的主要原因吗?

I am actually a Java developer and I am very curious about it.我其实是一名 Java 开发人员,对此我很好奇。 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. Paul 和 Jesper 已经建议 Java 在 JDK1.5 之后支持所有类似的锁 class,如 C/C++。 But if possible, I still wish someone can explain more details why Java provides enough support, we still cannot find a pure Java "oracle".但是如果可能的话,我还是希望有人能详细解释一下为什么 Java 提供了足够的支持,我们仍然找不到一个纯粹的 Java “oracle”。

EDIT:编辑:

Also, I want to add something interesting I learned from developer.com by Nasir Khan.另外,我想补充一些我从 Nasir Khan 的 developer.com 那里学到的有趣的东西。 Understanding Java Multithreading and Read-Write Locks .了解 Java 多线程和读写锁

Some topic in it.里面的一些话题。

  • The interaction of the shared main memory with the thread's local memory,共享主memory与线程本地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来自FileLock JavaDocs

File locks are held on behalf of the entire Java virtual machine.文件锁代表整个 Java 虚拟机持有。 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++. Java 中的文件锁定与 C/C++ 中的完全相同。

UPDATE更新
I find another interesting area to compare that is我发现另一个比较有趣的领域是
in C++, there is some thing like在 C++ 中,有类似的东西

atomic<int> x, y;


in JAVA we also have AtomicInteger .在 JAVA 我们也有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. Java 在大多数方面都略高于 C/C++,主要是由于 JVM 提供的抽象。 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.由于效率较低,因此对于某些效率非常重要的任务,C/C++ 是首选,正如您提到的那样。

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.我认为(由于 JVM 和更高级别的抽象)是 C/C++ 和 Java 之间差异的主要原因和来源,即线程的处理方式和其他差异只是这个主要差异的方面或后果。

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.特别是关于读写锁,Java 提供了使用它们的工具(如之前的评论中所指出的),并且很可能您可能想要使用的任何同步方法在 Java 中以某种方式可用或实现。 How the JVM translates this to OS calls and the efficiency of the result is a different matter. JVM 如何将其转换为操作系统调用,结果的效率是另一回事。

Java does provide read-write locks - http://download.oracle.com/javase/6/docs/api/java/util/concurrent/locks/ReadWriteLock.html . 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.如果您还没有,请查看 java.util.concurrent package。 I suspect Java's support is comparable to C's.我怀疑 Java 的支持可以与 C 相媲美。 There are also a number of web servers written in Java that use either multithreaded or async IO (NIO).还有许多用 Java 编写的 web 服务器,它们使用多线程或异步 IO (NIO)。

I believe Java has the lock you mentioned.我相信 Java 有你提到的锁。

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

And I recommend the book Java Concurrency in Practice to you if you're interested in this topic.如果您对此主题感兴趣,我向您推荐本书 Java 并发实践。

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

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