Return-Path: Delivered-To: apmail-harmony-commits-archive@www.apache.org Received: (qmail 12649 invoked from network); 23 May 2007 14:57:06 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 23 May 2007 14:57:06 -0000 Received: (qmail 54172 invoked by uid 500); 23 May 2007 14:57:06 -0000 Delivered-To: apmail-harmony-commits-archive@harmony.apache.org Received: (qmail 54153 invoked by uid 500); 23 May 2007 14:57:06 -0000 Mailing-List: contact commits-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list commits@harmony.apache.org Received: (qmail 54110 invoked by uid 99); 23 May 2007 14:57:06 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 May 2007 07:57:06 -0700 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: domain of xiaofeng.li@gmail.com designates 64.233.166.178 as permitted sender) Received: from [64.233.166.178] (HELO py-out-1112.google.com) (64.233.166.178) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 23 May 2007 07:56:58 -0700 Received: by py-out-1112.google.com with SMTP id a29so322984pyi for ; Wed, 23 May 2007 07:56:37 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=bIATHp6igfEPZ9Nnh56NhdnDLXlzb37KK433/bbdcfeyMrENIQyCuQ0zFYz4A3B/a5/AOau9ihsesTTaddoC0l1K+MeYJvDap8F5uEsrczmxw93xAgg2jXDj0zKP5RuQ1i6DFB0h4rhBa+1eq6u+jdVzMExBCvwKjZpuezDXqvA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=LVXTdWe0b24QJ+gDOvAmvujT1xNDwvYHjJOL97V6QbUy85Cn4gfhCmke06dvW2ZM9lFnIFkWIsLpsb4t8gS9EzLWXHeWSqeatPSF2SKw/vqKa8gFXVPFt9gDiH+Uv9/ezPT/JoPxu0BRhAYf7VXr1wcLIQTVqX6XtvLvCKOLtPw= Received: by 10.90.32.14 with SMTP id f14mr530478agf.1179932197273; Wed, 23 May 2007 07:56:37 -0700 (PDT) Received: by 10.90.113.3 with HTTP; Wed, 23 May 2007 07:56:37 -0700 (PDT) Message-ID: <9623c9a50705230756x783c64a5te80d2cd5ba937720@mail.gmail.com> Date: Wed, 23 May 2007 22:56:37 +0800 From: "Xiao-Feng Li" To: dev@harmony.apache.org, "Mikhail Markov (JIRA)" Subject: Re: [jira] Commented: (HARMONY-3883) [classlib]WeakHashMap.keySet().toArray() intermittently throws NoSuchElementException in java.lang.ThreadTest of DRLVM kernel test Cc: commits@harmony.apache.org In-Reply-To: <20936777.1179931757727.JavaMail.jira@brutus> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <27580776.1179382216322.JavaMail.jira@brutus> <20936777.1179931757727.JavaMail.jira@brutus> X-Virus-Checked: Checked by ClamAV on apache.org In that case, the java.lang.ThreadGroup.activeCount() should check if the thread object is NULL, before invoking its isAlive() method. Thanks, xiaofeng On 5/23/07, Mikhail Markov (JIRA) wrote: > > [ https://issues.apache.org/jira/browse/HARMONY-3883?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12498255 ] > > Mikhail Markov commented on HARMONY-3883: > ----------------------------------------- > > Hmm... null entries are allowed in the WeakHashMap. See the following valid code: > import java.util.WeakHashMap; > > public class Test { > public static void main(String[] args) throws Exception { > WeakHashMap map = new WeakHashMap(); > map.put("1", null); > map.put(null, null); > map.put("2", null); > Object[] keys = map.keySet().toArray(); > > for (int i = 0; i < keys.length; ++i) { > System.out.println("key[" + i + "] = " + keys[i]); > } > } > } > > So, the proposed idea does not work as it will remove valid null entry ... > > > [classlib]WeakHashMap.keySet().toArray() intermittently throws NoSuchElementException in java.lang.ThreadTest of DRLVM kernel test > > ---------------------------------------------------------------------------------------------------------------------------------- > > > > Key: HARMONY-3883 > > URL: https://issues.apache.org/jira/browse/HARMONY-3883 > > Project: Harmony > > Issue Type: Bug > > Components: Classlib > > Environment: win/linux 32/64 > > Reporter: Li-Gang Wang > > Attachments: H-3883.patch, H-3883_add_alt1.patch, H-3883_add_alt2.patch, WeakHashmapTest.java > > > > > > I found there is a design bug in WeakHashMap.keySet().toArray() . > > WeakHashMap.keySet().toArray() inherits AbstractCollection.toArray(). Its implementation is as follows: > > public Object[] toArray() { > > int size = size(), index = 0; > > Iterator it = iterator(); > > Object[] array = new Object[size]; > > while (index < size) { > > array[index++] = it.next(); > > } > > return array; > > } > > After assigning 'size()' to 'size', the 'while' loop expects 'size' elements in the iterator 'it'. But actually GC might happen and clear some weak keys of the WeakHashMap instance in the meantime. The iterator will skip the cleared weak keys in 'it.hasNext()' called by 'it.next()', so the actual number of the elements in 'it' is smaller than 'size'. When 'it' runs out its elements, 'it.next()' will throw a NoSuchElementException. Parts of HashIterator implementation in WeakHashMap is as follows: > > public boolean hasNext() { > > if (nextEntry != null) { > > return true; > > } > > while (true) { > > if (nextEntry == null) { > > while (position < elementData.length) { > > if ((nextEntry = elementData[position++]) != null) { > > break; > > } > > } > > if (nextEntry == null) { > > return false; > > } > > } > > // ensure key of next entry is not gc'ed > > nextKey = nextEntry.get(); > > if (nextKey != null || nextEntry.isNull) { > > return true; > > } > > nextEntry = nextEntry.next; > > } > > } > > public R next() { > > if (expectedModCount == modCount) { > > if (hasNext()) { > > currentEntry = nextEntry; > > nextEntry = currentEntry.next; > > R result = type.get(currentEntry); > > // free the key > > nextKey = null; > > return result; > > } > > throw new NoSuchElementException(); > > } > > throw new ConcurrentModificationException(); > > } > > I suspect the intermittent failure in java.lang.ThreadTest may disappear if this bug is fixed. > > -- > This message is automatically generated by JIRA. > - > You can reply to this email to add a comment to the issue online. > > -- http://xiao-feng.blogspot.com