Thread中的run()和start()的区别
来源:互联网 发布:淘宝 卖东西 编辑:程序博客网 时间:2024/05/17 08:58
啥都不用说,先上一个例子:
/** * @author I321023 * different between function run and function start in class Thread */public class TestRS extends Thread{ public TestRS(String name) { super(name); } @Override public void run() { System.out.println("Thread is running~"); } public static void main(String[] args) { TestRS tRs = new TestRS("My thread"); tRs.run();//Thread is running~ tRs.start();//Thread is running~ }}
对于上面这个简单的程序的输出结果大家应该都没有疑问,但是应该发现run(),start()最终的结果都是调用了run()方法,那他两是不是一样的呢?再来看一段代码:
/** * @author I321023 * * different between function run and function start in class Thread */public class TestRS extends Thread{ public TestRS(String name) { super(name); } @Override public void run() { System.out.println(Thread.currentThread().getName() + " is running!"); } public static void main(String[] args) { TestRS tRs = new TestRS("My thread"); tRs.run();//main is running! tRs.start();//My thread is running! }}
这里大家可以清楚的看到这两个的区别,下面总结一下:
1. tRs.run();//main is running!
是在主线程main中调用的,并没有新线程
2. tRs.start();//My thread is running!
start()方法会新启动一个线程,启动后,这个新线程会调用run()方法,这才是我们想要的多线程
下面我们来看一下这两个方法的源码:
/** * If this thread was constructed using a separate * <code>Runnable</code> run object, then that * <code>Runnable</code> object's <code>run</code> method is called; * otherwise, this method does nothing and returns. * <p> * Subclasses of <code>Thread</code> should override this method. * * @see #start() * @see #stop() * @see #Thread(ThreadGroup, Runnable, String) */ @Override public void run() { if (target != null) {//target是Runnable对象,如果不为空直接调用Runnable的成员方法run(),并不会新建线程 target.run(); } }
/** * Causes this thread to begin execution; the Java Virtual Machine * calls the <code>run</code> method of this thread. * <p> * The result is that two threads are running concurrently: the * current thread (which returns from the call to the * <code>start</code> method) and the other thread (which executes its * <code>run</code> method). * <p> * It is never legal to start a thread more than once. * In particular, a thread may not be restarted once it has completed * execution. * * @exception IllegalThreadStateException if the thread was already * started. * @see #run() * @see #stop() */public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0) throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); boolean started = false; try { start0();//调用本地方法start0() started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } private native void start0();//会新开一个线程,新线程再调用run()方法
PS:从上面两段源码,大家应该可以很清楚看到run()和start()的区别,当多线程的时候千万不要弄错了。。。
0 0
- Thread中的run()和start()的区别
- thread.start和 thread.run 的区别
- Thread.start()和Thread.run()的区别
- Thread的start和run的区别
- thread的run和start的区别
- Thread的start 和run 的区别
- Thread中start()和run()的区别
- Thread的start()和run()区别
- Thread中start()和run()的区别
- Thread的run和start方法区别
- Thread run()和start()方法的区别
- Thread中start()和run()的区别
- thread的run和start区别
- Thread中start()和run()的区别
- Thread的run()和start()区别
- Thread的start()和run()
- Thread的start和run
- Thread的run和start
- androidStudio 又添一个新武器 findBugs
- [leetcode-27]Remove Element(C语言)
- STM32 __asm bug
- Two output file names resolved to the same output path
- UI_Singleton(单例传值)
- Thread中的run()和start()的区别
- C++中浅拷贝和深拷贝问题
- swift Bool和元组(Turple)
- Go1.5 改进摘要
- 【cocos2d-x 3.X自学笔记】[转]Eclipse配置安卓开发环境(解决SDK manager下载慢问题)
- 文章标题
- Regular Expression Matching
- JavaScript学习(二)
- Android:控件WebView显示网页