JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label run(). Show all posts
Showing posts with label run(). Show all posts

Can we call run() method directly to start a new thread

7:52 AM
No, you can not directly call run method to start a thread. You need to call start method to create a new thread. If you call run method directly , it won’t create a new thread and it will be in same stack as main. Lets understand with the help of example: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31   package org . arpit . java2blog ; class CustomThread extends Thread {   public void run ( ) {    for ( int i = 0 ; i < 5 ; i ++ ) {    try {      Thread . sleep ( 300 ) ;    } catch ( InterruptedException e ) {         e . printStackTrace ( ) ;    }    System . out . println ( "Thread is running :" + i ) ;    }    }   }   public class StartThreadAgainMain {   public static void main ( String [ ] args ) {    CustomThread ct1 = new CustomThread ( ) ;    CustomThread ct2

.