package com.tao.ftpserver.Thread;
public class ThreadStyle extends Thread{
private String name;
public ThreadStyle(String name) {
this.name = name;
}
@Override
public void run() {
for(int i = 0; i < 10; i++) {
System.out.println(this.name+"--"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void run2() {
for(int i = 0; i < 10; i++) {
System.out.println(this.name+"--"+i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//方式1
ThreadStyle thread11 = new ThreadStyle("thread1");
thread11.run2();
ThreadStyle thread22 = new ThreadStyle("thread2");
thread22.run2();
//方式2
Thread thread1 = new ThreadStyle("thread1");
thread1.start();
Thread thread2 = new ThreadStyle("thread2");
thread2.start();
}
}
方式1:普通方式调用方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a0iSMPLd-1624285782057)(C:\Users\sunjiatao\AppData\Roaming\Typora\typora-user-images\image-20210621222643834.png)]](https://i-blog.csdnimg.cn/blog_migrate/7bf5150c1421a25290559e23c176521b.png)
多线程方式调用方法
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Jm77U8K-1624285782058)(C:\Users\sunjiatao\AppData\Roaming\Typora\typora-user-images\image-20210621222450742.png)]](https://i-blog.csdnimg.cn/blog_migrate/b55f7047835d328a9c81698a136a5b99.png)
下一篇,Runnble接口方式
知是行之始,行是知之成 !
本文介绍了如何使用Java的ThreadStyle类和Runnable接口实现线程,通过两种方式创建并启动线程,对比了普通调用与多线程方式的区别。读者将理解如何在Java中管理和并发执行任务。

被折叠的 条评论
为什么被折叠?



