java学习part25多线程

132-多线程-程序、进程、线程与并行、并发的概念_哔哩哔哩_bilibili

 1.概念

2.共享内容

只有线程间能通信,进程之间不共享内容。

 

3.继承thread的多线程

相当于golang里先写一个线程函数run(),子类对象调用start()相当于go关键字

相当于go run()

package thread;

//继承并且重写run
class PrintNumber extends Thread{
    @Override
    public void run() {
        for(int i=1;i < 100;i++){
            if(i%2==0){
                System.out.println(i);
            }
        }


    }
}

//多线程
//创建对象调用start()
public class EvenNumTest{
    public static void main(String[] args) {
        PrintNumber printNumber = new PrintNumber();
        printNumber.start();
        PrintNumber printNumber1 = new PrintNumber();
        printNumber1.start();
        for(int i=1;i < 100;i++){
            if(i%2==0){
                System.out.println(i + "aaa");
            }
        }


    }

}

tips:不能直接调用run(),必须start(),直接调run()是单线程,start()里实现了多线程

4.实现runnable接口来多线程

前面都一样,因为你没法写start()方法,所以把你写的类赋给thread,然后用thread实例start()开启线程

5.常用函数

5.1构造器

对于指定线程名的线程

5.2功能函数

6.生命周期

 jdk5.0之前

 jdk5.0之后

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java多线程下载网络图片的实现方法如下: 1. 定义一个DownloadTask类,用于表示一个下载任务,其中包含下载的URL、存储路径和线程数等信息。 2. 在DownloadTask类中实现Runnable接口,并重写run()方法,在该方法中实现下载图片的逻辑,可以使用URLConnection或者HttpClient等工具类进行网络请求,并将下载的数据写入本地文件。 3. 在DownloadTask类中定义一个download()方法,用于启动下载任务,该方法将根据线程数划分下载任务,并启动多个线程进行下载。 4. 在应用程序主方法中创建DownloadTask对象,并调用download()方法启动下载任务。 下面是一个简单的Java多线程下载网络图片的示例代码: ```java import java.io.*; import java.net.*; import java.util.concurrent.*; public class DownloadTask implements Runnable { private String url; private String file; private int threadCount; public DownloadTask(String url, String file, int threadCount) { this.url = url; this.file = file; this.threadCount = threadCount; } public void run() { try { URLConnection conn = new URL(url).openConnection(); int contentLength = conn.getContentLength(); System.out.println("Total size: " + contentLength); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.setLength(contentLength); ExecutorService executor = Executors.newFixedThreadPool(threadCount); for (int i = 0; i < threadCount; i++) { long start = (long) i * contentLength / threadCount; long end = (long) (i + 1) * contentLength / threadCount - 1; executor.execute(new DownloadThread(url, file, start, end)); } executor.shutdown(); executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES); System.out.println("Download completed."); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String url = "http://www.example.com/image.jpg"; String file = "image.jpg"; int threadCount = 4; DownloadTask task = new DownloadTask(url, file, threadCount); task.download(); } } class DownloadThread implements Runnable { private String url; private String file; private long start; private long end; public DownloadThread(String url, String file, long start, long end) { this.url = url; this.file = file; this.start = start; this.end = end; } public void run() { try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); InputStream in = conn.getInputStream(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.seek(start); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { raf.write(buffer, 0, len); } raf.close(); in.close(); System.out.println("Part downloaded: " + start + "-" + end); } catch (Exception e) { e.printStackTrace(); } } } ``` 在上述代码中,DownloadTask类表示一个下载任务,其中包含下载的URL、存储路径和线程数等信息。在run()方法中实现下载图片的逻辑,将根据线程数划分下载任务,并启动多个线程进行下载。DownloadThread类表示一个下载线程,其中包含下载的URL、存储路径和下载的起始和结束位置等信息。在run()方法中实现下载数据的逻辑,使用HttpURLConnection发送请求,并将下载的数据写入本地文件。 在应用程序主方法中,创建DownloadTask对象,并调用download()方法启动下载任务。其中,url表示要下载的图片URL,file表示要保存的本地文件路径,threadCount表示下载线程数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值