Dalvik VM& Java内存模型(Android上的并发编程) [英] Dalvik VM & Java Memory Model (Concurrent programming on Android)

查看:244
本文介绍了Dalvik VM& Java内存模型(Android上的并发编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事Android项目,涉及大量的并发编程,我将实现一些自定义的线程间通信东西( java.util.concurent 不适合我的目的)。



并发编程通常不容易,但Dalvik似乎更难。要获得正确的代码,你应该知道一些具体的事情,以及Dalvik出现问题。我只是无法找到有关Dalvik VM的详细文档。大多数Android资源(即使是 developer.android.com )专注于平台API,并且不提供关于一些非平凡(或低级别)内容的任何深入信息。



例如,Dalvik VM符合哪个版本的 Java语言规范?根据回答, volatile 变量的处理是不同的,并且影响使用 volatile 变量的任何并发代码。 p>

已有一些相关问题:





fadden 的一些答案非常有用,但我仍然想要更详细和完整地了解相关问题。



下面是我有趣的一个原始问题(如果有必要,我会更新列表,以前的问题的答案会到达):


  1. 在哪里可以找到关于Dalvik VM的详细信息,它可以提供以下问题的答案?

  2. Dalvik VM的 Java语言规范版本符合?

  3. 如果对(2)的回答是第三版,那么Dalviks对Java内存模型的支持在本规范中是否完整?特别是 volatile 变量的语义支持如何完整?

  4. Android中的双重锁定 fadden 提供以下评论:


    Yup。通过添加volatile关键字,这将在单处理器(所有版本的Android)和SMP(3.0蜂窝和更高版本)上工作


    这是否意味着具有双核CPU但只有Android 2.3的 Samsung Galaxy SII 可能会执行错误的并发代码? (当然Galaxy只是一个例子,问题是有任何多Android设备与Android Android平台)


  5. Dalvik的内存模型和Java一样吗? fadden 请提供以下句子的答案:


    目前没有关于JSR-133的Dalvik的运送版本是完全正确的


    这是否意味着任何现有的正确并发Java代码在发布此评论的任何Android版本上可能无法正常工作?




更新#1:回答@ gnat的评论(太久不能发表评论)



@gnat发布评论:


@Alexey Dalvik不符合任何JLS版本,因为一致性需要传递JCK不是Dalvik的选择。 这是否意味着你甚至不能应用标准的Java编译器,因为它符合标准规范?这么重要吗?如果是,如何?


好吧,我的问题是不明确的。我实际上的意思是,JLS 不仅是Java编译器实现的规则,而且是任何 JVM 实现的隐式指南。事实上,例如, JLS 表示读取和写入一些类型是原子操作。这对编译器编写器来说不是很有趣,因为读/写只是转换为单个操作码。但对于任何应该正确实现这些操作码的 JVM 实现来说是至关重要的。现在你应该看到我在说什么。虽然Dalvik接受并执行使用标准Java编译器编译的程序,但 没有任何保证 , (你可能期望的),因为没有人(除了Dalvik的开发人员)知道是否所有JLS的功能在程序中使用Dalvik支持。



很明显, em> JCK 不是Dalvik的一个选项,它是好的,但程序员真的应该知道他们可能依赖的 JLS Dalvik。但是在文档中没有任何关于这个的话。虽然你可能希望像=,+, - ,*等最简单的操作符是你期望的非平凡的特征 volatile 变量(在 JLS 的第二和第三版中有所不同)?后者不是在 JLS 中特别是在 Java内存模型中可能找到的最不平凡的东西。

解决方案

我没有完全阅读你的问题,
,但首先不要使用volatile,即使opengles编码不使用它不同的ui vs渲染线程。 >

使用volatile当且仅当一个线程写入(对某些类的静态属性)
和其他读取,即使那样你必须同步,处理计数的方法



如何在Java中运行不同类的实例的线程之间同步静态变量?


  1. 始终使用同步

  2. 不要在大项目中跳过并发编程等难题。

  3. 讨论了可运行的概念,
    处理程序,以及交换消息b / w线程(UI THREAD AND RENDERER THREAD)。


I am working on Android projects which involve the lot of concurrent programming and I am going to implement some custom inter-threads communication stuff (the one from java.util.concurent are not well suited for my purposes).

The concurrent programming is not easy in general but with Dalvik it seems to be even harder. To get the correct code you should know some specific things and that where problem arise with Dalvik. I just can't find a detailed documentation about the Dalvik VM. Most Android resources (even the developer.android.com focused on platform API and doesn't provide any deep information about some non-trivial (or low-level) things).

For example, to which edition of Java Language Specification the Dalvik VM is conform ? Depending of answer the treatment of volatile variables are different and affect the any concurrent code which use the volatile variables.

There are already some related questions:

and some answers by fadden are very useful but I still want to get more detailed and complete understanding of matter in question.

So below a raw questions I am interesting in (I will update the list if necessary as answers for previous questions will arrive):

  1. Where to find the details about the Dalvik VM which may provide the answers for questions below ?
  2. To which edition of Java Language Specification the Dalvik VM is conform to ?
  3. If answer to (2) is "third edition" then how complete the Dalviks's support of Java Memory Model defied in this specification ? And especially how complete the support for semantic of volatile variables ?
  4. In the Double checked locking in Android the fadden provide the following comment:

    Yup. With the addition of the "volatile" keyword, this will work on uniprocessor (all versions of Android) and SMP (3.0 "honeycomb" and later)

    Does it mean that Samsung Galaxy SII which has the dual-core CPU but only Android 2.3 may execute the concurrent code incorrectly ? (of course Galaxy is only an example, the question is about of any multicore device with pre-Android 3.0 platform)

  5. In the Is Dalvik's memory model the same as Java's? the fadden provide the answer with the following sentence:

    No currently-shipping version of Dalvik is entirely correct with respect to JSR-133

    Does it mean that any existing correct concurrent Java code may work incorrectly on any Android version released up to date of posting of this comment ?

Update#1: Answer to @gnat's comment (too long to be comment too)

@gnat post a comment:

@Alexey Dalvik does not conform to any JLS edition, because conformance requires passing JCK which is not an option for Dalvik. Does it mean that you even can't apply standard Java compiler because it conform to standard specification ? does that matter? if yes, how?

Well, my question was somehow ambiguous. What I actually meant is that JLS is not only the rules for Java compiler implementations but also an implicit guidelines for any JVM implementations. Indeed, JLS, for example, states that reading and writing of some types are atomic operations. It is not very interesting for compiler writer because read/write translated just into a single opcodes. But it is essential for any JVM implementation which should implement these opcodes properly. Now you should see what I am talking about. While Dalvik accept and execute the programs compiled with standard Java compiler there are no any guaranties that they are executed correctly (as you may expect) just because no one (except maybe Dalvik's developers) knows if all JLS's features used in the program are supported by Dalvik.

It is clear that JCK is not an option for Dalvik and it is Ok, but programmers really should know on which features of JLS they may rely when execute their code on Dalvik. But there is no any words about this in documentation. While you may expect that simplest operators like =, +, -, *, etc. are works as you expect what about non-trivial features like semantic of volatile variables (which is different in 2nd and 3rd editions of JLS)? And latter is not the most non-trivial things you may find in JLS and particular in Java Memory Model.

解决方案

I haven't read your question completely, but first of all do not use volatile, even opengles coders do not use it for different ui vs renderer threads.

Use volatile if and only if one thread writes (say to some class' static property) and other reads, even then you have to synchronize, read this for some good ways to handle counts

How to synchronize a static variable among threads running different instances of a class in java?

  1. always use synchronize
  2. do not jump at large projects for such difficult topics like concurrent programming
  3. go through android samples on games where they have discussed the concept of runnables, handlers, and exchanging messages b/w threads (UI THREAD AND RENDERER THREAD).

这篇关于Dalvik VM& Java内存模型(Android上的并发编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆