for the above program with respective to program no.4 the output generated is "1" but wht my question is we know that when a thread encounters "run" method in the program a separate thread will be generated which executes the code in the "run" method but in the above program if that is the case a separate thread should be generated and should execute the code in the "run" method and before it happens "System.out.println(a.i)" should print the output as "0"
Hi, for the above programs how the output is generated,anybody please provide some explanation for the above [ April 29, 2005: Message edited by: Barry Gaunt ]
when a thread encounters "run" method in the program a separate thread will be generated which executes the code in the "run" method but in the above program if that is the case a separate thread should be generated
No, when a thread's run method is called, a separate thread of execution is not started. A separate thread of execution is started only when u call start() method on it. So, in ur case the behavior is as of a normal program.
1) Just based on my reasoning above, a separate thread will be started only if u call start on it. So u can see that u calling run method on A directly, no new thread is running, its the B thread which runs and so u get the first two outputs having thread name as that of B. For the last line, a new thread is being created and so u get the given output.
2) In this case i guess u r getting a compiler error. If u have read overriding u would have got the explanation. U r trying to override the method run() and throwing broader checked exceptions than run() method in Thread class. Infact, the run() method in Class Thread doesnt throw any exception, so when u override it u shouldnt throw any checked exception.