From owner-freebsd-java Tue Oct 1 5:18:27 2002 Delivered-To: freebsd-java@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id C95D937B401 for ; Tue, 1 Oct 2002 05:18:25 -0700 (PDT) Received: from keyser.soze.com (keyser.soze.com [194.165.93.196]) by mx1.FreeBSD.org (Postfix) with ESMTP id 63C7143E7B for ; Tue, 1 Oct 2002 05:18:25 -0700 (PDT) (envelope-from stefan.arentz@soze.com) Received: by keyser.soze.com (Postfix, from userid 1000) id AA157351B2; Tue, 1 Oct 2002 13:57:01 +0200 (CEST) Date: Tue, 1 Oct 2002 13:57:01 +0200 From: Stefan Arentz To: Giulio Ferro Cc: freebsd-java@FreeBSD.ORG Subject: Re: Wrong thread behavior Message-ID: <20021001115701.GA36825@keyser.soze.com> References: <3D99524C.50605@libero.it> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3D99524C.50605@libero.it> User-Agent: Mutt/1.3.99i Sender: owner-freebsd-java@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org On Tue, Oct 01, 2002 at 09:44:12AM +0200, Giulio Ferro wrote: > Native jdk 1.3.1p7 > > If we consider the following snippet: > > public class SimpleThread extends Thread { > private int countDown = 50; > private static int threadCount = 0; > private int threadNumber = ++threadCount; > public SimpleThread() { > System.out.println("Making " + threadNumber); > } > public void run() { > while(true) { > System.out.println("Thread " + > threadNumber + "(" + countDown + ")"); > if(--countDown == 0) return; > } > } > public static void main(String[] args) { > for(int i = 0; i < 5; i++) > new SimpleThread().start(); > System.out.println("All Threads Started"); > } > } ///:~ > > > You can notice that the execution never breaks out of run() unless you > explicitly put a sleep() or yield(). > It goes on any single thread until the thread returns without > automatically switching to the others. The 50 iterations are so fast that they are all executed before the next thread is started. Try setting countDown to 100000 and then see what it does. Here on a P4/1.6Ghz it takes a few seconds before the threads start running concurrent. Tested with: java version "1.3.1-p7" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-p7-root-020929-22:36) Classic VM (build 1.3.1-p7-root-020929-22:36, green threads, nojit) and: java version "1.3.1-p7" Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-p7-root-020929-22:36) Java HotSpot(TM) Client VM (build 1.3.1-internal, mixed mode) Note to all; please specify the output of java -version when you submit a bug :) S. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-java" in the body of the message