File tree Expand file tree Collapse file tree
JavaBase/src/com/xinan/thread01 Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .xinan .thread01 ;
2+
3+ /**
4+ *
5+ * 实现Runnable接口,并重写该接口的run()方法,该run()方法同样是线程执行体,创建Runnable实现类的实例,
6+ * 并以此实例作为Thread类的target来创建Thread对象,该Thread对象才是真正的线程对象。
7+ *
8+ */
9+ public class MyRunnable implements Runnable {
10+
11+ @ Override
12+ public void run () {
13+ for (int i = 0 ; i < 100 ; i ++) {
14+ System .out .println (Thread .currentThread ().getName () + i );
15+ }
16+ }
17+
18+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .thread01 ;
2+
3+ /**
4+ * 通过直接继承Thread实现创建线程,重写run()方法
5+ * @author Administrator
6+ *
7+ */
8+ public class MyThread extends Thread {
9+ private int i =0 ;
10+ @ Override
11+ public void run () {
12+ for (i =0 ;i <100 ;i ++){ //其返回对当前正在执行的线程对象的引用。
13+ System .out .println (Thread .currentThread ().getName ()+"--" +i );
14+ }
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .xinan .thread01 ;
2+
3+ public class ThreadTest {
4+ public static void main (String [] args ){
5+ for (int i =0 ;i <100 ;i ++){
6+ System .out .println ();
7+ }
8+ }
9+ }
You can’t perform that action at this time.
0 commit comments