1

Please help me out to understand how does run method gets called by calling start method of thread class.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
  • What exactly do you mean by "how"? It's just part of the implementation - you don't need to worry about it, and the low-level details may well be OS-specific. The important point is that it happens on a different thread of execution. – Jon Skeet Feb 28 '13 at 07:08
  • possible [duplicate](http://stackoverflow.com/questions/8052522/why-we-call-thread-start-method-which-in-turns-calls-run-method) – Rahul Feb 28 '13 at 07:11
  • You have the java-ee tag on this question: there are very few occasions where you should be creating and starting threads in a Java EE container – Andrew Alcock Feb 28 '13 at 07:12

4 Answers4

4

The start() method starts a new thread of execution, and arranges things so that the new thread of execution invokes the run() method. The exact mechanics are OS-specific.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

I would suggest to look at the source code of java.lang.Thread.start() method. Its a synchronized method which in turn calls private native method and then the OS specific threading mechanism takes over (which eventually calls the run() method of the current object)

Santosh
  • 17,667
  • 4
  • 54
  • 79
1

From docs

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

Community
  • 1
  • 1
rajesh
  • 3,247
  • 5
  • 31
  • 56
0

Thread start() call run() is an internal process, while threading is platform dependent.

Following is what Java developers say

java.​lang.​Thread
public synchronized void start()
Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).
It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.
Throws:
IllegalThreadStateException - if the thread was already started. 
See Also:
Thread.run(), Thread.stop()

You sure don't have to worry about this. If you are seeking for an thread example, here is one

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ThreadEx extends JFrame
{
    private JLabel numTxt;
    private JButton click;
    private Thread t = new Thread(new Thread1());

    public ThreadEx()
    {
        numTxt = new JLabel();
        click = new JButton("Start");
        click.addActionListener(new ButtonAction());

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new FlowLayout());
        centerPanel.add(numTxt);
        centerPanel.add(click);

        this.add(centerPanel,"Center");

        this.pack();
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private class Thread1 implements Runnable
    {

        @Override
        public void run() 
        {
            try
            {
                for(int i=0;i<100;i++)
                {
                    numTxt.setText(String.valueOf(i));
                    Thread.sleep(1000);
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }

    }

    private class ButtonAction implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            t.start();
        }

    }

    public static void main(String[]args)
    {
        new ThreadEx();
    }
}
PeakGen
  • 21,894
  • 86
  • 261
  • 463