• Post Reply Bookmark Topic Watch Topic
  • New Topic

can anyone help me to understand multithreading  RSS feed

 
Bobby Marvikuan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello can anyone help me to understand multithreading I am not getting the point. Firstly,the first line of an output of the program below is The main thread dies here. Why it starting from there. Second question, there are while(true) loop so, it should never end unless the exception is thrown but the program does not throws an exception. So, why it starts then executing this:new Thread (new SleepyThread(5, "1 ")).start(); and the last question why it is doing more 2 thread than first is it because of the program says to or ... smth else? Thanks in advance!
 
Tim Moores
Saloon Keeper
Posts: 4036
94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly,the first line of an output of the program below is The main thread dies here. Why it starting from there

Newly created threads #1 and #2 both sleep at the beginning, so it's natural to run the non-sleeping main thread. But even without that it's up to the JVM to schedule threads. You shouldn't expect thread X to start being executed before thread Y just because it was created earlier; it may work out that way (and probably will), but not necessarily so. You could insert print statements at the beginning of the run method to see when each thread starts to execute.

Second question, there are while(true) loop so, it should never end unless the exception is thrown but the program does not throws an exception. So, why it starts then executing this:new Thread (new SleepyThread(5, "1 ")).start();

I don't understand this question; can you rephrase it? What did you observe happening, and what were you expecting instead?

and the last question why it is doing more 2 thread than first is it because of the program says to or ... smth else?

Is it? It's pretty evenly distributed on my machine. Of course, thread #2 waits for a shorter time than thread #1 at startup, which explains the consecutive 2s at the beginning.
 
Bobby Marvikuan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
Firstly,the first line of an output of the program below is The main thread dies here. Why it starting from there

Newly created threads #1 and #2 both sleep at the beginning, so it's natural to run the non-sleeping main thread. But even without that it's up to the JVM to schedule threads. You shouldn't expect thread X to start being executed before thread Y just because it was created earlier; it may work out that way (and probably will), but not necessarily so. You could insert print statements at the beginning of the run method to see when each thread starts to execute.

Second question, there are while(true) loop so, it should never end unless the exception is thrown but the program does not throws an exception. So, why it starts then executing this:new Thread (new SleepyThread(5, "1 ")).start();

I don't understand this question; can you rephrase it? What did you observe happening, and what were you expecting instead?

and the last question why it is doing more 2 thread than first is it because of the program says to or ... smth else?

Is it? It's pretty evenly distributed on my machine. Of course, thread #2 waits for a shorter time than thread #1 at startup, which explains the consecutive 2s at the beginning.

Thank you so much!
and second question was
while(true){
try { Thread.sleep(500);; } catch (InterruptedException e){System.out.print("I was interrapted"); }
System.out.print(msg+" ");
}

this loop should be endless isn't it? because true is not becoming false and there is no return statement in there??
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bobby Marvikuan wrote:
Firstly,the first line of an output of the program below is The main thread dies here. Why it starting from there.

you spun off two threads that are told to first sleep for at least 1 second, The main thread is able to continue working while your two threads are sleeping and so it finishes its work first and prints your statement.

Bobby Marvikuan wrote:Second question, there are while(true) loop so, it should never end unless the exception is thrown but the program does not throws an exception. So, why it starts then executing this:new Thread (new SleepyThread(5, "1 ")).start();

I think you are asking why thread #2 can execute while thread #1 is still working and that is the whole point of threads - you can have more than one executing the same code at the same time.

Bobby Marvikuan wrote:and the last question why it is doing more 2 thread than first is it because of the program says to or ... smth else? Thanks in advance!

you told each thread to sleep st * 1000 so any difference in printing would relate to the st variable you pass in, you see?

 
Bobby Marvikuan
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a lot!!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!