A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. 通过创建两个线程和主线程交替运行,理解多线程的概念和运行
  2. 首先创建一个类,继承Thread
  3. 然后复写Thread的run方法
  4. 最后在主函数中new两个该类的对象,并调用start方法,启动线程



  5. package com.mytest;
  6. public class test03 {
  7. public static void main(String[] args) {
  8.   Test t1 = new Test();
  9.   Test t2 = new Test();
  10.   t1.start();
  11.   t2.start();
  12.   for (int x = 0; x < 60; x++) {
  13.    System.out.println("main______" + x);
  14.   }
  15. }
  16. }
  17. class Test extends Thread {
  18. public void run() {
  19.   for (int x = 0; x < 60; x++) {
  20.    System.out.println("text run...." + x);
  21.   }
  22. }
  23. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马