当前位置:网站首页>多线程——Runnable接口,龟兔赛跑
多线程——Runnable接口,龟兔赛跑
2022-07-25 09:27:00 【看小虫子】
下载图片
使用common-io-2.2 jar包下载图片
- 先下载jia包,
- 解压后粘贴到当前目录下,
- 在将其添加到类库中就可以使用了
package ThreadDemo;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
//联系Thread实现多线程同步下载图片
public class ThreadDemo01 extends Thread {
private String url; //网络图片的地址
private String name;//保存的文件名
public ThreadDemo01(String url, String name) {
this.url = url;
this.name = name;
}
//下载图片线程的执行体
@Override
public void run() {
webDownLoader web=new webDownLoader();
web.downloader(url,name);
System.out.println("下载了文件名为"+name);
}
public static void main(String[] args) {
ThreadDemo01 th1=new ThreadDemo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","n.jpg");
ThreadDemo01 th2=new ThreadDemo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","m.jpg");
ThreadDemo01 th3=new ThreadDemo01("https://img2.baidu.com/it/u=1814268193,3619863984&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1658682000&t=d20c88a4eebf882f4f8950f8613ac1b4","p.jpg");
th1.start();
th2.start();
th3.start();
}
//下载器
class webDownLoader {
//下载方法
public void downloader(String url,String name){
try {
FileUtils.copyURLToFile(new URL(url),new File(name));
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO异常"+"downLoader出现问题");
}
}
}
}
Runnable接口的使用
- 实现接口Runnable具有多线程能力
- 启动线程:传入对象+Thread对象.start();
- 推荐使用:避免了单继承的局限性,灵活方便,方便同一个对象被多个线程使用
package RunnableStudy;
//创建线程方式二:实现Runnable接口,重写run方法,指向线程需要丢入runnable接口实现类
//调用start
public class Demo01 implements Runnable{
public static void main(String[] args) {
//创建runnable接口的实现类对象
Demo01 demo=new Demo01();
//创建线程对象,通过线程对象来开启我们的线程
// Thread thread = new Thread(demo);
// thread.start();
//两行结合
new Thread(demo).start();
for (int i = 0; i < 1200; i++) {
System.out.println("我在听歌");
}
}
@Override
public void run() {
for (int i = 0; i < 200; i++) {
System.out.println("我在学习");
}
}
}
龟兔赛跑
package RunnableStudy;
public class Race implements Runnable {
private static String winner;
public static void main(String[] args) {
Race race=new Race();
new Thread(race,"乌龟").start();
new Thread(race,"兔子").start();
}
@Override
public void run() {
for (int i = 0; i <=100; i++) {
//模拟兔子休息
if (Thread.currentThread().getName().equals("兔子")&&i%10==0){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//判断比赛是否结束
boolean flag=GameOver(i);
//比赛结束了停止程序
if(flag){
break;
}
System.out.println(Thread.currentThread().getName()+"跑了"+i+"步");
}
}
private boolean GameOver(int steps){
//判断是否有胜利者
if(winner!=null){
//已经存在胜利者,比赛结束
return true;
}else if(steps>=100){
winner=Thread.currentThread().getName();
System.out.println("获胜者是"+winner);
return true;
}
return false;
}
}
边栏推荐
猜你喜欢

See how a junior student of double non-2 (0 Internship) can get an offer from Alibaba and Tencent

SD/SDIO/EMMC

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

Use of dictionary tree

CCF 201509-4 Expressway

车辆属性最近一次入库时间初始化生成sql脚本文件

小程序调起微信支付

概率机器人学习笔记第二章

Internal structure of SOC chip

DHCP的配置(以华为eNSP为例)
随机推荐
【建议收藏】靠着这些学习方法,我入职了世界五百强——互联网时代的“奇技淫巧”
[recommended collection] with these learning methods, I joined the world's top 500 - the "fantastic skills and extravagance" in the Internet age
Detailed explanation of MySQL database
IDEA整体字体大小修改
阿里MQTT物联网平台“云产品流转”实战——两片ESP32通过物联网平台实现远程互操作
力扣刷题组合问题总结(回溯)
字典树的使用
mysql历史数据补充新数据
Introduction to arm GIC
TM1638 LED数码显示模块ARDUINO驱动代码
Use of dictionary tree
CCF 201503-4 network delay
CentOS install redis
VScode配置ROS开发环境:修改代码不生效问题原因及解决方法
第五阶段第一周
概率机器人学习笔记第二章
Probability theory and mathematical statistics 4 continuous random variables and probability distributions (Part 1)
Advanced introduction to digital IC Design SOC
salt常见问题
[RNN] analyze the RNN from rnn- (simple|lstm) to sequence generation, and then to seq2seq framework (encoder decoder, or seq2seq)