Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 1 Oct 2002 13:57:01 +0200
From:      Stefan Arentz <stefan.arentz@soze.com>
To:        Giulio Ferro <aurynkid@libero.it>
Cc:        freebsd-java@FreeBSD.ORG
Subject:   Re: Wrong thread behavior
Message-ID:  <20021001115701.GA36825@keyser.soze.com>
In-Reply-To: <3D99524C.50605@libero.it>
References:  <3D99524C.50605@libero.it>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021001115701.GA36825>