首页 > 其他分享 >Thread

Thread

时间:2022-11-17 23:59:11浏览次数:37  
标签:TestThread2 Thread String 线程 public name

Thread

  • 自定义线程类继承Thread类
  • 重写run()方法,编写线程执行体
  • 创建线程对象,调用start()方法启动线程
package com.deng.demo01;


//创建线程方式一:继承Thread类,重写run()方法,调用start开启线程

//总结:注意,线程开启不一定立即执行,由cpu调度执行
public class TestThread1 extends Thread {
    @Override
    public void run() {
        //run方法线程体
        for (int i = 0; i < 100; i++) {
            System.out.println("我在看代码-"+i);
        }
    }

    public static void main(String[] args) {
        //main线程,主线程

        //创建一个线程对象
        TestThread1 testThread1 = new TestThread1();

        //调用start()方法开启线程
        testThread1.start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("我在学习多线程--"+i);
        }
    }
}
  • 案例:下载图片
package com.deng.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//练习Thread,实现多线程同步下载图片
public class TestThread2 extends Thread {

    private String url;//网络图片地址
    private String name;//保存的文件名

    public TestThread2(String url, String name) {
        this.url = url;
        this.name = name;
    }

    //下载图片线程的执行体
    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("下载了文件名为:" + name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("http://alifei05.cfp.cn/creative/vcg/800/version23/VCG219486b1842.jpg", "1.jpg");
        TestThread2 t2 = new TestThread2("http://tenfei02.cfp.cn/creative/vcg/800/version23/VCG41175510742.jpg","2.jpg");
        TestThread2 t3 = new TestThread2("http://alifei05.cfp.cn/creative/vcg/800/version23/VCG21gic5474154.jpg","3.jpg");
        
        t1.start();
        t2.start();
        t3.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方法出现问题");
        }
    }
}

注:下载的图片来源于网络

标签:TestThread2,Thread,String,线程,public,name
From: https://www.cnblogs.com/dengovo/p/16901818.html

相关文章

  • 线程--thread
    线程:线程是系统分配给内核的最小单元,线程是进程的一部分。特点:1.一个进程可以包含多个线程2.线程也是一个运行行为,消耗计算机资源3.一个进程中的所有线程共享这个进程......
  • kafka报错:Exception in thread “main“ joptsimple.UnrecognizedOptionException: zo
    kafka创建主题topic报错:Exceptioninthread"main"joptsimple.UnrecognizedOptionException:zookeeperisnotarecogn如图: 原因:kafka最新版本创建主题topic方式已......
  • jdk11源码-Thread
    一DemopublicclassThreadTest00{publicstaticvoidmain(String[]args){Threadt0=newThread(()->System.out.println("bizthread..."),"t0......
  • c++多线程thread用法小例子
    测试分布式存储系统时,针对并发测试,同时创建500个文件,采用这种方法。#include<iostream>#include<thread>usingnamespacestd;voidproc(inta){cout<<"子线......
  • Could not obtain transaction-synchronized Session for current thread
    一场景我的项目中,发生这个错误是由于我使用springmvc框架,但是在里面新建了一个springboot的camunda(流程)模块。而springmvc使用hibernate,camunda使用jpa的entityManager......
  • Netty源码-02-FastThreadLocalThread
    一DemopublicclassFastThreadLocalTest00{privatefinalstaticFastThreadLocal<Long>v=newFastThreadLocal<Long>(){@Overrideprotec......
  • Java并发之回顾Thread和runnable
    jdk文档的描述ThreadAthreadisathreadofexecutioninaprogram.TheJavaVirtualMachineallowsanapplicationtohavemultiplethreadsofexecutionrunn......
  • Java中ThreadLocal详解
    一、ThreadLocal简介        ThreadLocal叫做线程变量,意思是ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的......
  • Thread的状态变更
      【需注意的是:运行中(Running)和就绪(Ready)并不是Java的线程状态】publicenumState{NEW,RUNNABLE,BLOCKED,WAITING,......
  • python 线程池 ThreadPoolExecutor
    从Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor(线程池)和ProcessPoolExecutor(进程池)两个类。相比threading等模块,该模......