Well I can't speak on behalf of the JVM, but what it does when it enters a break mode, or how it schedules its threads are completely in its own world.<div><br></div><div>Multitasking isn't really any different now than it was back then... Pre-emptive multitasking is simply cooperative multitasking, but with the added concept of automatic yielding after time slices, and that thread is so short, it will probably not exceed a single OS timeslice. As soon as it enters the debugger state, all threads will halt.</div>
<div>I expect exactly what you see personally from the operating system. I have no experience with Java, but I wouldn't base any opinions about computer software behaviour on what Java does. It lives in its own universe.</div>
<div><br></div><div>If you spawned those threads, and then entered a loop doing something fairly busy on the main thread, it will exceed its timeslice within a short time, and then the other threads would have their turn. Your program is just too short to predictably visualise the OS threading behaviour.</div>
<div><br></div><div>Run this:</div><div><br></div><div>It should 'tick' for a while, and you'll see the other threads slip in within a short time as they get given time by the scheduler.</div><div><br></div><div>
 <span style>int main(string[] argv)</span><br style><span style>{</span><br style><span style>   DerivedThread dt1 = new DerivedThread();</span><br style><span style>   dt1.start();</span><br style><br style><span style>   DerivedThread dt2 = new DerivedThread();</span><br style>
<span style>   dt2.start();</span><br style><br style><span style>   DerivedThread dt3 = new DerivedThread();</span><br style><span style>   dt3.start();</span><br style><br>   for(int i=0; i<1000000; ++i)</div><div>      <span style>writeln("tick");</span></div>
<div><br style><span style>   Thread.sleep(dur!("seconds")( 4 ));</span><br style><span style>   return 0;</span><br style><span style>}</span> <br><br><div class="gmail_quote">On 8 February 2012 08:56, Oliver Puerto <span dir="ltr"><<a href="mailto:saxo123@gmx.de">saxo123@gmx.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Manu,<br>
<br>
thanks for taking some time to answer. I just wrote a little Java program that does just the same:<br>
<br>
public static void main(String[] args)<br>
{<br>
    Thread thread1 = new Thread(new HelloWorldThread());<br>
    thread1.start();<br>
<br>
    Thread thread2 = new Thread(new HelloWorldThread());<br>
    thread2.start();<br>
<br>
    Thread thread3 = new Thread(new HelloWorldThread());<br>
    thread3.start();<br>
<br>
    System.out.println("done");<br>
}<br>
<br>
"Hello world" is printed to the console as soon as I jump in the Java eclipse debugger over any thread?.start(). I have a breakpoint on the last sysout("done") line to prevent the program to terminate before I'm done with seeing things in the debugger. So there is no single Thread.sleep here and no yielding. This is the behavior I would expect from my D program as well.<br>

<br>
This somehow reminds me of older versions of VisualWorks Smalltalk where multi-threading was not preemptive, but cooperative: threads with same priority do not interrupt each other but run till completion unless one of the threads does a Processor.yield.<br>

<br>
When your programm was irresponsible in a certain situation you added some more Processor.yield to the code and things were better. But at the same time the additional yields caused some more incorrectly synchronized access to shared data to cause race conditions. Without people that do not understand concurrency well this was not easy ...<br>

<br>
I still don't understand why my D program behaves as described in my initial post and not like the corresponding Java program. Is this really the debugger?<br>
<br>
Cheers, Oliver<br>
<br>
-------- Original-Nachricht --------<br>
> Datum: Wed, 8 Feb 2012 01:01:22 +0200<br>
> Von: Manu <<a href="mailto:turkeyman@gmail.com">turkeyman@gmail.com</a>><br>
> An: "digitalmars.D" <<a href="mailto:digitalmars-d@puremagic.com">digitalmars-d@puremagic.com</a>><br>
> Betreff: Re: Output to console from DerivedThread class strange<br>
<div class="HOEnZb"><div class="h5"><br>
> I'm not sure why you are asking me about this?<br>
><br>
> The problem I think is with your understanding of the debugger, and sleep<br>
> states of other threads.<br>
> If you break on a breakpoint to step the program, all threads be stopped.<br>
> If you step the code one line at a time in the debugger, it will not yield<br>
> to the other threads while stepping, the new thread will not begin until<br>
> the current thread has a reason to yield, and you won't see anything.<br>
> When you step over the sleep, the program will execute the sleep, which<br>
> will then cause a wait for some moments, immediately handing the execution<br>
> to another thread for that time, and during this time the other thread<br>
> will<br>
> have opportunity to print their message and exit. Sleep will always yield<br>
> the current thread immediately, even if you sleep for 0.<br>
> With the sleep calls commented out like that, the main thread will not<br>
> reach a point where it will be forced to yield to another thread, so you<br>
> won't see it until the end.<br>
><br>
> What I expect to happen, without breaking to the debugger, with all sleeps<br>
> commented out; the 3 prints will happen after the program exits (it will<br>
> take some time before the main thread yields to the new created threads)<br>
> ..<br>
> if you uncomment any of those sleeps, all prints coming before it will<br>
> print at that time...<br>
><br>
> On 8 February 2012 00:28, Oliver Puerto <<a href="mailto:saxo123@gmx.de">saxo123@gmx.de</a>> wrote:<br>
><br>
> > Hello,<br>
> ><br>
> > I have a question concerning threading. I use Visual Studio with the<br>
> > Visual-D plugin. The problem is somehow that when executing the code<br>
> below<br>
> > "Derived thread running." is displayed 3 times on the console but not<br>
> > before "return 0" is reached. Then "Derived thread running." is<br>
> displayed<br>
> > 3x all of a sudden, but not each one by one after each other. This looks<br>
> a<br>
> > bit strange to me.<br>
> ><br>
> > When I unquote the Thread.sleeps after every d?.start() "Derived thread<br>
> > running." is displayed immediately on the console when stepping over<br>
> > Thread.sleep with the debugger. When stepping over d*.start() still<br>
> nothing<br>
> > happens regardless how much time I wait in the debugger till I jump to<br>
> the<br>
> > next line. I can't make really head or tail of this. I would expect<br>
> > "Derived thread running." to appear on the console somewhen after<br>
> > d?.start() was executed. Thereafter I could do an additional<br>
> Thread.sleep<br>
> > if I wanted to. But this would not be necessary for any "Derived thread<br>
> > running." message to be displayed on the console.<br>
> ><br>
> > I believe there is something with the debugger I don't understand or<br>
> don't<br>
> > realize. Any suggestions/ideas?<br>
> ><br>
> > Thank you, Oliver Plow<br>
> ><br>
> ><br>
> > import std.stdio, core.thread;<br>
> ><br>
> > import DerivedThread;<br>
> ><br>
> > int main(string[] argv)<br>
> > {<br>
> >    DerivedThread dt1 = new DerivedThread();<br>
> >    dt1.start();<br>
> >    // Thread.sleep(dur!("seconds")( 1 ));<br>
> ><br>
> >    DerivedThread dt2 = new DerivedThread();<br>
> >    dt2.start();<br>
> >    // Thread.sleep(dur!("seconds")( 1 ));<br>
> ><br>
> >    DerivedThread dt3 = new DerivedThread();<br>
> >    dt3.start();<br>
> >    // Thread.sleep(dur!("seconds")( 1 ));<br>
> ><br>
> >    Thread.sleep(dur!("seconds")( 4 ));<br>
> >    return 0;<br>
> > }<br>
> ><br>
> > ------------------ DerivedThread.d ------------------<br>
> ><br>
> > import std.stdio, core.thread;<br>
> ><br>
> > class DerivedThread : Thread {<br>
> >        this() {<br>
> >                super( &run );<br>
> >        }<br>
> ><br>
> > private :<br>
> >        void run() {<br>
> >                writeln("Derived thread running.");<br>
> >        }<br>
> > }<br>
> > --<br>
> > Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir<br>
> > belohnen Sie mit bis zu 50,- Euro! <a href="https://freundschaftswerbung.gmx.de" target="_blank">https://freundschaftswerbung.gmx.de</a><br>
> ><br>
<br>
--<br>
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir<br>
belohnen Sie mit bis zu 50,- Euro! <a href="https://freundschaftswerbung.gmx.de" target="_blank">https://freundschaftswerbung.gmx.de</a><br>
</div></div></blockquote></div><br></div>