进阶day05-多线程启动方式一:继承Thread类
This commit is contained in:
16
javaSE-day05/src/com/inmind/thread07/MyThread.java
Normal file
16
javaSE-day05/src/com/inmind/thread07/MyThread.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.inmind.thread07;
|
||||||
|
|
||||||
|
public class MyThread extends Thread{
|
||||||
|
|
||||||
|
//默认无参构造方法
|
||||||
|
/*public MyThread(){
|
||||||
|
super();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {//重写的run方法的作用:当前线程要执行的任务
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
System.out.println("hello pyhton");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
javaSE-day05/src/com/inmind/thread07/ThreadDemo02.java
Normal file
23
javaSE-day05/src/com/inmind/thread07/ThreadDemo02.java
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package com.inmind.thread07;
|
||||||
|
/*
|
||||||
|
在java中使用一个类Thread来表示一个线程.
|
||||||
|
|
||||||
|
启动多线程的方式一:
|
||||||
|
1.定义一个子类继承Thread
|
||||||
|
2.重写run方法
|
||||||
|
3.创建子类对象,调用start方法启动
|
||||||
|
*/
|
||||||
|
public class ThreadDemo02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//开启一个新的线程执行
|
||||||
|
MyThread myThread = new MyThread();
|
||||||
|
// myThread.run();//不能直接调用run方法,否则不启动多线程
|
||||||
|
//注意:start方法的作用是,真的启动线程,启动起来的新线程的任务是执行该线程类的run方法
|
||||||
|
myThread.start();
|
||||||
|
|
||||||
|
for (int i = 0; i < 1000; i++) {
|
||||||
|
System.out.println("hello java");
|
||||||
|
}
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user