Why Threads don't run in concurrency?

M

mrcode

Hi all, i'm a newbie of java and i have a question.
I've typed this small program about Threads but don't work how i aspect:

//nomefile: Conteggio.java

public class Conteggio extends Thread{

int FineConteggio;

Conteggio(String Nome, int n){
super(Nome);
FineConteggio=n;
}

public void run(){
System.out.println(this.getName()+":sto contando");
for (int i=0; i<=FineConteggio; i++)
System.out.println(this.getName()+": "+i);
System.out.println(this.getName()+":fine conteggio!");
}
}

//nomefile: MarioAnna.java

class MarioAnna{

public static void main(String argv[]){

Conteggio Mario=new Conteggio("Mario",10);
Conteggio Anna=new Conteggio("Anna",5);

Anna.start();
Mario.start();
}
}

form command prompt i've typed "java MarioAnna" and this is the result:

C:\provejava>java MarioAnna
Anna:sto contando
Anna: 0
Anna: 1
Anna: 2
Anna: 3
Anna: 4
Anna: 5
Anna:fine conteggio!
Mario:sto contando
Mario: 0
Mario: 1
Mario: 2
Mario: 3
Mario: 4
Mario: 5
Mario: 6
Mario: 7
Mario: 8
Mario: 9
Mario: 10
Mario:fine conteggio!

Why? where i've make errors? I don't understand.....Anna.start() don't
return until finsihed? Why the "Mario" thread don't start to count in
concurrency with the "Anna" thread?....:-(
thanks a lot
Regards
Marco

PS: sorry for my poor english :p
 
G

Gordon Beaton

Hi all, i'm a newbie of java and i have a question.
I've typed this small program about Threads but don't work how i
aspect:

The exact behaviour depends on platform dependent scheduling
characteristics that Java does not specify and your application should
not depend on.

In your example your threads don't have very much work to do, so
"Anna" has probably finished executing before you even manage to start
"Mario". Try larger numbers.

/gordon
 
S

Stefan Ukena

Hi Marco,

like Gordon said, it depends on your OS. You could also try using sleep():

...
System.out.println(this.getName()+":sto contando");
for (int i=0; i<=FineConteggio; i++)
System.out.println(this.getName()+": "+i);
System.out.println(this.getName()+":fine conteggio!");
try {
sleep(6000);
}
catch(InterruptedException e) {
// While asleep, another
// thread might interrupt us.
// This will trigger an exception.
}
}
...

This will put your thread to sleep for 6000 milli-seconds in every pass
of your loop and give other threads a chance to run.

If you will be doing serious thread-programming you will have to check
the different behaviour on Linux und Win-based systems!

Good luck,
Stefan
 
M

mrcode

It's true!! Larger number made the apps working how i aspect, and also the
sleep() metod work fine.
Another brick in my java-wall.... :)
Thanks for the patience.
Regards
Marco
 
P

Phil...

also if all of the threads are the same priority
there is no need to switch to another thread as
long as the currently executing one has some work
to do
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top