- 通过创建两个线程和主线程交替运行,理解多线程的概念和运行
- 首先创建一个类,继承Thread
- 然后复写Thread的run方法
- 最后在主函数中new两个该类的对象,并调用start方法,启动线程
-
-
-
- package com.mytest;
- public class test03 {
- public static void main(String[] args) {
- Test t1 = new Test();
- Test t2 = new Test();
- t1.start();
- t2.start();
- for (int x = 0; x < 60; x++) {
- System.out.println("main______" + x);
- }
- }
- }
- class Test extends Thread {
- public void run() {
- for (int x = 0; x < 60; x++) {
- System.out.println("text run...." + x);
- }
- }
- }
复制代码 |
|