aspose file tools
The moose likes Java in General and the fly likes can anyone help me to understand multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "can anyone help me to understand multithreading" Watch "can anyone help me to understand multithreading" New topic
Author

can anyone help me to understand multithreading

Bobby Marvikuan
Ranch Hand

Joined: Mar 14, 2012
Posts: 54
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
Rancher

Joined: Sep 21, 2011
Posts: 2407
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

Joined: Mar 14, 2012
Posts: 54
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

Joined: Apr 30, 2003
Posts: 819

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

Joined: Mar 14, 2012
Posts: 54

Thanks a lot!!!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: can anyone help me to understand multithreading
 
Similar Threads
thread and exception!
Aboutt Daemon Threads
Strange Error [java.lang.ArrayIndexOutOfBoundsException]
closing thread and its childs