diff --git a/javaSE-day05/src/com/inmind/thread07/MyThread.java b/javaSE-day05/src/com/inmind/thread07/MyThread.java new file mode 100644 index 0000000..3bf2383 --- /dev/null +++ b/javaSE-day05/src/com/inmind/thread07/MyThread.java @@ -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"); + } + } +} diff --git a/javaSE-day05/src/com/inmind/thread07/ThreadDemo02.java b/javaSE-day05/src/com/inmind/thread07/ThreadDemo02.java new file mode 100644 index 0000000..d8fb2ea --- /dev/null +++ b/javaSE-day05/src/com/inmind/thread07/ThreadDemo02.java @@ -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("程序结束"); + } +}