简体   繁体   中英

Thread class how start method calls the run method of sub class

请帮助我了解如何通过调用线程类的start方法来调用run方法。

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.

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 )

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).

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM