多线程理解

多线程理解

java中的线程:java中,每个线程都有一个调用栈存放在线程栈之中,一个java应用总是从main()函数开始运行,被称为主线程。一旦创建一个新的线程,就会产生一个线程栈。线程总体分为:用户线程和守护线程,当所有用户线程执行完毕的时候,JVM自动关闭。但是守候线程却不独立于JVM,守候线程一般是由操作系统或者用户自己创建的。

为什么要有线程?

​ 每个进程都有自己的地址空间,即进程空间。一个服务器通常需要接收大量并发请求,为每一个请求都创建一个进程系统开销大、请求响应效率低,因此操作系统引进线程。

线程的四种创建方法

/**

  • 线程的创建方式一
  • extends Thread类
  • @author Administrator

*1.线程就绪(准备开启)方法是run还是start?
*start()方法
*
*2.调用run()方法和调用start()方法是有什么区别?
*调用start()方法的时候,此时线程是一个就绪状态(可运行)但是并没有
*直接运行,而是等待cpu的时间片,得到时间片之后就会调用run方法执行
*线程体,run方法执行结束,就是线程结束。
*
*/
public class Thread01 extends Thread{

/*
 * run()方法线程的执行体
 * **/
@Override
public void run() {
	System.out.println("当前执行的线程是"+Thread.currentThread().getName());
	for (int i = 0; i < 100; i++) {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(i);
	}
	
}

public static void main(String[] args) {

	
	Thread01 thread01 = new Thread01();
	thread01.start();//开启线程
	
	Thread01 thread02 = new Thread01();
	thread02.start();//开启线程
	
	Thread01 thread03 = new Thread01();
	thread03.start();//开启线程

	
}

}

2

package com.hp.test;
/**

  • 线程创建方式二
  • 实现接口 Runnable
  • @author Administrator

*/
public class Thread02 implements Runnable{

@Override
public void run() {
	//获取当前执行线程的名字
	System.out.println("开启:"+Thread.currentThread().getName());
	for (int i = 0; i < 3; i++) {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("////");
	}
	System.out.println("结束:"+Thread.currentThread().getName());
}


public static void main(String[] args) {

	Thread02 thread02 = new Thread02();
	
	//线程一
	Thread t1 = new Thread(thread02);
	t1.start();
	
	//线程二
	Thread t2 = new Thread(thread02);
	t2.start();
	
	//线程三
	Thread t3 = new Thread(thread02);
	t3.start();
	
}

}

3

package com.hp.test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**

  • 实现Callable接口
  • 有返回值,并且run方法变成了call方法
  • @author Administrator

*/
public class Thread03 implements Callable{

public static void main(String[] args) throws Exception {
	FutureTask<String> task = new FutureTask<String>(new Thread03());
	
	Thread thread = new Thread(task);
	thread.setName("线程1");
	thread.start();
	
	
	
	String result = task.get();//获取返回值
	System.out.println(result);
	
	
	
}

@Override
public String call() throws Exception {
	// TODO Auto-generated method stub
	System.out.println("当前线程:"+Thread.currentThread().getName());
	return "hello Thread";
}

}

4

package com.hp.test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
*

  • 线程池方式
  • @author Administrator

*/
public class Thread04 {

public static void main(String[] args) throws InterruptedException, ExecutionException {
	//初始化线程池
	ExecutorService service = Executors.newFixedThreadPool(2);//参数2表示初始化2个线程

	//执行实现Runnable接口的线程
	service.execute(new Thread1());
	//执行实现Callable接口的线程
	Future<Integer> result =service.submit(new Thread2());
	//返回值
	Integer resultValue = result.get();
	System.out.println(resultValue);

	//卸载(释放资源)
	service.shutdown();
}

}

class Thread1 implements Runnable
{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println(“我是实现了Runnable接口完成的线程”);
}
}

class Thread2 implements Callable{

@Override
public Integer call() throws Exception {
	// TODO Auto-generated method stub
	System.out.println("我是实现了Callable接口完成的线程");
	return 200;
}

}

多线程下载图片

package com.hp.test;

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

import org.apache.commons.io.FileUtils;

public class FileDownload extends Thread {

private String url;
private String FileName;

public String getUrl() {
	return url;
}

public String getFileName() {
	return FileName;
}

public FileDownload(String url,String FileName)
{
	this.url=url;
	this.FileName=FileName;
	
}

@Override
public void run() {
	try {
		DownLoad(getUrl(), getFileName());
	} catch (MalformedURLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}


//下载图片
public static void DownLoad(String url,String FileName) throws MalformedURLException, IOException
{
	FileUtils.copyURLToFile(new URL(url), new File(FileName));
	
	
}

}

package com.hp.test;

public class TestDownLoad {

/**
 * 多线程下载图片
 * @param args
 */
public static void main(String[] args) {
	FileDownload img1 = new FileDownload("http://img3m2.ddimg.cn/13/24/1703862382-1_l_5.jpg", "1.jpg");
	FileDownload img2 = new FileDownload("http://img3m8.ddimg.cn/48/33/1900707978-1_l_2.jpg", "2.jpg");
	FileDownload img3 = new FileDownload("http://img3m4.ddimg.cn/43/6/1901232574-1_l_1.jpg", "3.jpg");


	img1.start();
	img2.start();
	img3.start();
}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值