Skip to content

Commit 4ddc834

Browse files
author
xiachaochao
committed
11
1 parent 6372f78 commit 4ddc834

3 files changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)