Thread 中的run()

 1 /**
 2      * If this thread was constructed using a separate
 3      * <code>Runnable</code> run object, then that
 4      * <code>Runnable</code> object's <code>run</code> method is called;
 5      * otherwise, this method does nothing and returns.
 6      * <p>
 7      * Subclasses of <code>Thread</code> should override this method.
 8      *
 9      * @see     #start()
10      * @see     #stop()
11      * @see     #Thread(ThreadGroup, Runnable, String)
12      */
13     @Override
14     public void run() {
15         if (target != null) {
16             target.run();
17         }
18     }

  1.run方法本身就是thread的一种方法,本身不作任何操作。

  2.当传入target时,则调用target的run方法,target是runnable对象。

  3.当没有传入target,则需要重写Thread的run方法实现业务。

  4.当执行thread的run方法,并不会创建新的子线程,而是调用main主线程。即当run中执行完成后才能运行下面的代码。

严谨的来说:创建线程只有一种方式就是构造Thread类,实现线程的执行单元run有两种方式,一是重写thread的run方法,二是构造thread时传入Runnable对象,实现Runnable的run方法。

  

猜你喜欢

转载自www.cnblogs.com/fengyue0520/p/10422008.html