一个应用程序可以有多个进程
一个进程可以有多个线程
线程和进程区别
即是—个进程从创建、运行到消亡的过程。
进程:是指一个内存中运行的应用程序,每个进程都有一个独立的内存空间,一个应用程序可以同时运行多个进程;进程也是程序的一次执行过程,是系统运行程序的基本单位;系统运行一个程序
线程:进程内部的一个独立执行单元;一个进程可以同时并发的运行多个线程,可以理解为一个进程便相当于一个单CPU操作系统,而线程便是这个系统中运行的多个任务。
并发与并行
并行:指两个或多个事件在同–时刻发生(同时发生)。
并发:指两个或多个事件在同一个时间段内发生。
多线程使用场景:
1.后台任务,比如游戏服务器
2.定时向大量用户(100W)用户发邮件
3.异步处理:发微博/记录日志等
4.分布式计算…
进程:
一个运行的程序或代码就是一一个进程,一个没有运行的代码叫程序。进程是系统进行
资源分配的最小单位,进程拥有自己的内存空间,所以进程间数据不共享,开销大。
实例1.从网上下载到本地
package com.hp.Work4;
import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoadThread extends Thread{
private int threadId;//线程编号
private int startIndex;//开始位置
private int endIndex;//结束位置
public DownLoadThread(int threadId, int startIndex, int endIndex) {
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
URL InterPath = new URL("https://images.unsplash.com/photo-1499754162586-08f451261482?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60");//统一资源管理器
HttpURLConnection httpURLConnection = (HttpURLConnection)InterPath.openConnection();//开启网络打开url连接 HttpURLConnection
//可不写 连接网络相关属性
httpURLConnection.setRequestMethod("GET");//请求头的方法
httpURLConnection.setConnectTimeout(5000);//设置网络连接超时时间
httpURLConnection.setReadTimeout(5000);//设置读取超时时间
//线程下载范围开始位置到结束位置 格式关键字必须一样range范围 bytes字节
httpURLConnection.setRequestProperty("Range","bytes="+startIndex+"-"+endIndex);
//网络状态码: 500服务器错误 404访问页面不存在 206部分内容准备就绪 200 ok
int responseCode = httpURLConnection.getResponseCode();//获取返回编码
if (responseCode==206){
InputStream inputStream = httpURLConnection.getInputStream();//获取到下载流的信息
File file = new File("NBA.png");//设置本地下载到的位置
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");//输出流 名字 mode权限
//指定从哪个位置开始写数据
randomAccessFile.seek(startIndex);
//字节流输入
//设置字节大小
byte [] bytes = new byte[1024];
//定义长度len
int len = 0;
//while循环输入
while ((len=inputStream.read(bytes))!=-1){
randomAccessFile.write(bytes,0,len);
}
//关流
inputStream.close();
randomAccessFile.close();
//测试输出当前线程作用的文件下载的大小位置输出
System.out.println("线程编号"+threadId+"下载完成,开始位置"+startIndex+"结束位置"+endIndex);
//定义一个变量当一个线程结束时++,用于同步当前线程使用
//静态变量通过类直接调用
TestThreadDown.finishedThread++;
synchronized (TestThreadDown.str) {
TestThreadDown.finishedThread=0;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.hp.Work4;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestThreadDown {
static int finishedThread=0;//定义一个变量当一个线程结束时++
static String str="";//线程锁参数
static int threadCount=3;//线程数量
public static void main(String[] args) throws Exception {
URL InterPath = new URL("https://images.unsplash.com/photo-1499754162586-08f451261482?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60");//统一资源管理器
HttpURLConnection httpURLConnection = (HttpURLConnection)InterPath.openConnection();//开启网络打开url连接 HttpURLConnection
//可不写 连接网络相关属性
httpURLConnection.setRequestMethod("GET");//请求头的方法
httpURLConnection.setConnectTimeout(5000);//设置网络连接超时时间
httpURLConnection.setReadTimeout(5000);//设置读取超时时间
//获取当前下载文件的长度
int contentLength = httpURLConnection.getContentLength();
//每个线程处理文件的大小
int size = contentLength/threadCount;
//使用循环将使用3个线程执行下载
for (int threadid = 0; threadid < threadCount; threadid++) {
int startIndex = threadid*size;//单线程开始位置
int endIndex = (threadid+1)*size;//单线程结束位置
DownLoadThread downLoadThread = new DownLoadThread(threadid, startIndex, endIndex);
downLoadThread.start();
}
}
}