多线程-继承Thread

    科技2024-03-10  73

    package com.lzy.thread; /** * 创建线程方式一: * 1、创建:继承Thread+重写run * 2、启动:创建子类对象+start * @author Administration * */ public class aStartThread extends Thread{ /** * 线程入口点 * */ @Override public void run() { for(int i=0;i<20;i++) { System.out.println("一边studying"); } } public static void main(String[] args) { //创建子类对象 aStartThread st=new aStartThread(); //启动 st.start(); for(int i=0;i<20;i++) { System.out.println("一边play"); } } }

    15行run()方法为线程入口点,如果在main()方法中直接调用run()方法而不是用start()方法,则不会发生多线程,运行后“一边studying”执行完才会执行“一边play”。或者将26 27行放到22行上面,运行后”一边play“执行完才会执行“一边studying”。

    Processed: 0.014, SQL: 8