1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

'Main' Thread in Java

Discussion in 'Java' started by Sanskruti, May 6, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India

    Introduction



    The 'main()' method in Java is referred to the thread that is running, whenever a Java program runs. It calls the main thread because it is the first thread that starts running when a program begins. Other threads can be spawned from this main thread. The main thread must be the last thread in the program to end. When the main thread stops, the program stops running.

    Main thread is created automatically, but it can be controlled by the program by using a Thread object. The Thread object will hold the reference of the main thread with the help of currentThread() method of the Thread class.

    Listing shows how main thread can be controlled by a program:

    Code:
    class MainThread 
    {
    	public static void main(String args [] ) 
    	{
    		Thread t = Thread.currentThread ( );
    
    		System.out.println ("Current Thread : " + t);
    		System.out.println ("Name : " + t.getName ( ) );
    		System.out.println (" ");
    
    		t.setName ("New Thread");
    		System.out.println ("After changing name");
    		System.out.println ("Current Thread : " + t);
    		System.out.println ("Name : " + t.getName ( ) );
    		System.out.println (" ");
    
    		System.out.println ("This thread prints first 10 numbers");
    		
    		try 
    		{
    			for (int i=1; i<=10;i++) 
    			{
    				System.out.print(i);
    				System.out.print(" ");
    				Thread.sleep(1000);
    			}
    		} 
    		catch (InterruptedException e) 
    		{
    			System.out.println(e); 
    		}
    	}
    }
    
    The output of this program is given below:

    Code:
    [COLOR=Blue]
    Current Thread : Thread[main,5,main]
    Name : main
    
    After changing name
    Current Thread : Thread[New Thread,5,main]
    Name : New Thread
    
    This thread prints first 10 numbers
    1 2 3 4 5 6 7 8 9 10
    [/COLOR]
    Let us now understand the working of the program in the listing. The program first creates a Thread object called 't' and assigns the reference of current thread (main thread) to it. So now main thread can be accessed via Thread object 't'. This is done with the help of currentThread() method of Thread class which return a reference to the current running thread. The Thread object 't' is then printed as a result of which you see the output Current Thread : Thread [main,5,main]. The first value in the square brackets of this output indicates the name of the thread, the name of the group to which the thread belongs. The program then prints the name of the thread with the help of getName() method.

    The name of the thread is changed with the help of setName() method. The thread and thread name is then again printed. Then the thread performs the operation of printing first 10 numbers. When you run the program you will that the system wait for sometime after printing each number. This is caused by the statement Thread.sleep (1000). The sleep() method can also used in another way in which it accepts two arguments. The first argument represents the amount of milliseconds and the second argument represents the amount of nanoseconds. Effectively, the thread will sleep for specified milliseconds + nanoseconds. Its (sleep() method's) prototype is as follows:
    Code:
    static void sleep(long milliseconds, int nanoseconds)
     
  2. dilip.klose

    dilip.klose New Member

    Joined:
    Mar 10, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    static methods

    in java a static method can access only static data members but main() is a static method
    still it is accessing the instance data members by creating an object of other class.
    how can it be possible?" ;)
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    You need to revisit on how static members work.

    Any static members can be accessed via the object of the class but additionally they can be accessed even without creating the object
     
  4. swaran

    swaran New Member

    Joined:
    Mar 7, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    where this main thread actually present???
     
  5. swaran

    swaran New Member

    Joined:
    Mar 7, 2008
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    where is this main thread is actually present from where it is automatically called????
     

Share This Page