s-day05-多线程的实现(方式一)
This commit is contained in:
@@ -0,0 +1,22 @@
|
|||||||
|
package com.inmind.thread07;
|
||||||
|
/*
|
||||||
|
多线程的实现(方式一)
|
||||||
|
在java中使用一个类Thread,来表示一个线程
|
||||||
|
启动多线程的方式一:
|
||||||
|
1.定义一个子类继承Thread
|
||||||
|
2.重写run方法
|
||||||
|
3.创建子类对象,调用start方法才能启动线程
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//创建子线程对象
|
||||||
|
MyThread myThread = new MyThread();
|
||||||
|
//启动线程
|
||||||
|
myThread.start();
|
||||||
|
System.out.println("-------------");
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
System.out.println("hello world");
|
||||||
|
}
|
||||||
|
System.out.println("程序结束");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.inmind.thread07;
|
||||||
|
|
||||||
|
//自定义线程类
|
||||||
|
public class MyThread extends Thread{
|
||||||
|
@Override
|
||||||
|
public void run() {//线程任务代码
|
||||||
|
//打印100次"hello java"
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
System.out.println("hello java");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user