将Java编译成类似GO代码的行为。

huangapple go评论113阅读模式
英文:

Compile Java to behave like GO code

问题

是否有可能编写一个Java编译器或虚拟机,使您能够以与GO程序编译相同的方式编译使用线程和阻塞系统调用的传统Java应用程序。

因此,new Thread().run(); 将创建轻量级线程,并且所有阻塞系统调用将改为异步操作系统调用,并使轻量级线程让出。

如果不可能,主要原因是什么!

英文:

Would it be possible to write a Java compiler or Virtual Machine that would let you compile legacy java application that use thread and blocking system call the same way GO program are compiled.

Thus new Thread().run(); would create light weight thread and all blocking system call will instead be asynchronous Operating System call and make the light weight thread yield.

If not, what is the main reason this would be impossible!

答案1

得分: 6

早期的Sun的Java运行时在Solaris(和其他UNIX系统)上使用了一个称为“绿色线程”的用户空间线程系统。如Java 1.1 for Solaris文档中所述:

实现多对一模型(多个用户线程对一个内核线程)允许应用程序创建任意数量的可以并发执行的线程。在多对一(用户级线程)实现中,所有线程活动都限制在用户空间。此外,一次只有一个线程可以访问内核,因此操作系统只知道一个可调度实体。因此,这种多线程模型提供了有限的并发性,并且不利用多处理器。Java线程在Solaris系统上的初始实现是多对一的,如下图所示。

很早就用操作系统的线程支持来替代了这种方式。在Solaris 9之前的Solaris版本中,这是一个类似于Go的M:N“多对多”系统,其中线程库在较少数量的内核级线程上调度了一些程序线程。在像Linux和使用1:1系统的较新版本的Solaris上,用户线程直接对应于内核级线程,情况就不同了。

我认为自那时以来,没有任何严肃的计划将Sun/Oracle JVM从使用本地线程库中移出。正如历史所显示的,JVM确实可以使用这样的模型,但似乎并没有被认为是值得追求的方向。

英文:

Earlier versions of Sun's Java runtime on Solaris (and other UNIX systems) made use of a user space threading system known as "green threads". As described in the Java 1.1 for Solaris documentation:

> Implementations of the many-to-one model (many user threads to one kernel thread) allow the application to create any number of threads that can execute concurrently. In a many-to-one (user-level threads) implementation, all threads activity is restricted to user space. Additionally, only one thread at a time can access the kernel, so only one schedulable entity is known to the operating system. As a result, this multithreading model provides limited concurrency and does not exploit multiprocessors. The initial implementation of Java threads on the Solaris system was many-to-one, as shown in the following figure.

This was replaced fairly early on by the use of the operating system's threading support. In the case of Solaris prior to Solaris 9, this was an M:N "many to many" system similar to Go, where the threading library schedules a number of program threads over a smaller number of kernel-level threads. On systems like Linux and newer versions of Solaris that use a 1:1 system where user threads correspond directly with kernel-level threads, this is not the case.

I don't think there has been any serious plans to move the Sun/Oracle JVM away from using the native threading libraries since that time. As history shows, it certainly would be possible for a JVM to use such a model, but it doesn't seem to have been considered a direction worth pursuing.

答案2

得分: 2

James Henstridge已经对Java green threads提供了很好的背景介绍,并且解释了将本机操作系统线程暴露给程序员会引入效率问题,因为使用它们是昂贵的。

已经有几个大学尝试从这种情况中恢复过来。其中两个是肯特大学的JCSP和Twente的CTJ(尽管可能已经不再活跃)。两者都提供了基于Hoare的CSP的Go风格的并发设计简化。但是,由于JVM线程昂贵,它们都受到了JVM性能差的影响。

如果性能不是关键,CSP是实现并发设计的一种更好的方式,因为它避免了异步编程的复杂性。你可以在生产代码中使用JCSP - 我就是这么做的。

有报道称,JCSP团队还有一个实验性的JNI插件,可以修改线程语义以提高效率,但我从未见过它的实际效果。

幸运的是,对于Go语言,你可以“一举两得”。你既可以获得基于CSP的“happen-before”简洁性,又可以获得顶级性能。太棒了!

另外:一篇有趣的牛津大学论文报告了对并发Scala程序进行延续传递风格修改的方法,使得可以在JVM上使用CSP。我希望在今年8月的牛津CPA2014会议上能听到更多关于这方面的消息(请原谅我打广告!)。

英文:

James Henstridge has already provided good background on Java green threads, and the efficiency problems introduced by exposing native OS threads to the programmer because their use is expensive.

There have been several university attempts to recover from this situation. Two such are JCSP from Kent and CTJ (albeit probably defunct) from Twente. Both offer easy design of concurrency in the Go style (based on Hoare's CSP). But both suffer from the poor JVM performance of coding in this way because JVM threads are expensive.

If performance is not critical, CSP is a superior way to achieve a concurrent design because it avoids the complexities of asynchronous programming. You can use JCSP in production code - I do.

There were reports that the JCSP team also had an experimental JNI-add-on to the JVM to modify the thread semantics to be much more efficient, but I've never seen that in action.

Fortunately for Go you can "have your cake and eat it". You get CSP-based happen-before simplicity, plus top performance. Yay!

Aside: an interesting Oxford University paper reported on a continuation-passing style modification for concurrent Scala programs that allows CSP to be used on the JVM. I'm hoping for further news on this at the CPA2014 conference in Oxford this August (forgive the plug!).

huangapple
  • 本文由 发表于 2014年3月10日 10:05:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/22290937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定