当前位置:网站首页>黑马程序员方式一实现多线程
黑马程序员方式一实现多线程
2022-07-19 05:16:00 【闲易生事】
/*
* 目标:多线程的创建方式一:继承Thread类实现
*/
public class ThreadDome1 {
public static void main(String[] args) {
// 3、new一个新线程对象
Thread thread=new Mythread();
// 4、调用start方法启动线程(执行的还是run方法)
thread.start();
// 5、执行main方法中主线程
for (int i = 0; i < 5; i++) {
System.out.println("主线程执行输出"+i);
}
}
}
/*
1、定义一个线程类继承Thread
*/
class Mythread extends Thread{
/*
2、覆写run方法,里面是定义线程以后要干啥
* */
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("子线程执行输出"+i);
}
}
}
1.方式一是如何实现多线程的?
* 继承Thread类
* 重写run方法
* 创建线程对象
* 调用start()方法启动
2.优缺点是什么?
* 优点:代码简单
* 缺点:存在单继承的局限性,线程类继承Thread后,不能继承其他类,不便于扩展。
3.注意
* 不要将主线程放在子线程之前,不然系统会优先执行主线程,不会达到多线程的目的。
边栏推荐
猜你喜欢
离散数据(数组)的过零位置搜索
Qt解决大量重复数据的操作
When FPM generates packages, the associated Allegro cannot generate packages after it is opened. Solution to the problem
Network Security Learning (XX) ACL
How to set the oil on the through hole cover when exporting the Gerber file of PCB
After ise14.7 was installed in win10 system, the software flashed back and the driver could not be installed normally
Verilog HDL language summary (full)
2022-7-11 Gu Yujia's study notes of group 8 (JS)
网络安全学习(十一)扫描与爆破
网络安全学习(十六)
随机推荐
CSV intro
UltraEdit line break / tab settings
Allegro cannot display drill legend information when adding drill legend
第三届“传智杯”全国大学生IT技能大赛(初赛B组题解)
经纬度及其与坐标系的转换
After ise14.7 was installed in win10 system, the software flashed back and the driver could not be installed normally
Qt解决大量重复数据的操作
equals和“==”的异同
firewall-cmd
乐山师范程序设计大赛2020-A: 好数对
计算机408+数据库【适合考研复试或期末复习】
Leetcode:2. 两数相加【大数加法+模拟进位】
計算機408+數據庫【適合考研複試或期末複習】
巴特沃斯(Butterworth)滤波器的设计和幅频特性曲线绘制
Unity实用框架(三)事件系统
方向信号的表达——复指数信号
上海二工大 - 健康日报AutoCheck
Stm32f4 PWM capture (rising edge / falling edge / high and low level time) detailed explanation (including code)
使用google cloud部署基于flask的网站
网络安全学习(十一)扫描与爆破