12345678910111213141516171819202122232425262728293031 |
- package com.sf.javase.thread;
- public class MyThread1 extends Thread {
- int i;
- // 继承是用子类重写了父类的run方法 就是实际的执行逻辑
- @Override
- public void run() {
- // 获取线程名字
- System.out.println(getName() + " begin");
- for (; i < 10; i++) {
- System.out.println(Thread.currentThread().getName() + ": " + i);
- }
- }
- public static void main(String[] args) {
- for (int i = 0; i < 30; i++) {
- System.out.println(Thread.currentThread().getName() + ": " + i);
- if (i == 10) {
- Thread t1 = new MyThread1();
- t1.start();
- // 在获取资源后 真正执行的是run方法
- }
- if (i == 20) {
- Thread t2 = new MyThread1();
- t2.start();
- }
- }
- }
- }
|