简单的多线程应用

本文探讨了Java中实现多线程的三种方式:继承Thread类、实现Runnable接口以及实现Callable接口。通过实例展示了启动线程前后的情况,并解释了为何要使用Runnable接口以避免单继承的限制,以及Callable接口在需要返回值和处理异常时的优势。
摘要由CSDN通过智能技术生成

实现方式一 继承Thread

需求主线程打印1-1000,子线程实现拷贝
public class ThreadDemo01 {
	
    public static void main(String[] args) {
    	//创建线程对象
    	CopyThread cp = new CopyThread(new File("01.mp4"), 
				new File("test.mp4"));
		//启动线程
    	cp.start();
    	
    	//调用打印方法
    	printNum();
	}

    //打印1到1000
	private static void printNum() {
		for (int i = 1; i <=1000; i++) {
			System.out.println("主方法"+i);
		}
		
	}
}

 自定义类继承thread 接口
 @author lang
 重写run方法
 
//实现文件拷贝
class  CopyThread extends Thread {
	//这俩个需要通过外界传入
	private File srcFile;
	private File descFile;
		
	public CopyThread() {
		super();
	}
  
	public CopyThread(File srcFile, File descFile) {
		super();
		this.srcFile = srcFile;
		this.descFile = descFile;
	}


	//次方法相当于 一个主方法,调用文件拷贝方法
	 public void run() {
		 //调用拷贝文件方法
		copyFile(srcFile, descFile);

	}

	//文件拷贝方法
	 public void copyFile(File srcFile,File descFile){
		 BufferedInputStream bis = null;
		 BufferedOutputStream bos = null;
		 
		 try {
			 //获取字节输入流
			bis = new BufferedInputStream(new FileInputStream(srcFile));
			 //获取字节输出流
			bos = new BufferedOutputStream(new FileOutputStream(descFile));			
			int len = 0;
			//每次读取1024字节
			byte[] bys = new byte[1024];		
			while((len =bis.read(bys))!= -1){
				bos.write(bys,0,len);
				bos.flush();
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			//关闭资源
			if(bos!=null){
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(bis!=null){
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	 }
	 
	public File getSrcFile() {
		return srcFile;
	}

	public void setSrcFile(File srcFile) {
		this.srcFile = srcFile;
	}

	public File getDescFile() {
		return descFile;
	}

	public void setDescFile(File descFile) {
		this.descFile = descFile;
	}

启动线程前

在这里插入图片描述

启动线程后

在这里插入图片描述在这里插入图片描述

方式二 实现Runnable 接口 重写run 方法

有了继承Thread 为什么还要有 实现Runable 接口呢,实现接口方式的好处,可以避免由于Java单继承带来的局限性。package thread;
//多线程实现方法二
public class ThreadDemo02{
 
	public static void main(String[] args) {
		//创建MyRunale对象
		MyRunnable mr = new MyRunnable(1, 100);
		//创建Thre
	    Thread thread = new Thread(mr);
	    //启动线程
	    thread.start();
	    
	    for (int i = 1; i <= 100; i++) {
			System.out.println("main:"+i);
		}
	}
	}
实现方式二 实现Runable接口,重写run 方法
     class MyRunnable implements Runnable {
    private int n;
    private int m;
    	
	public MyRunnable() {
		super();
	}

	public MyRunnable(int n, int m) {
		super();
		this.n = n;
		this.m = m;
	}

   //重写run方法

	public void run() {
		//计算 n-m的和
		int sum =0;
		for (int i = n; i <= m; i++) {
			sum += i;
		}
		System.out.println(n+"--"+m+"的和为"+sum);
	}
	
	public int getN() {
		return n;
	}

	public void setN(int n) {
		this.n = n;
	}

	public int getM() {
		return m;
	}

	public void setM(int m) {
		this.m = m;
	}
运行结果

在这里插入图片描述

三实现Callable接口

有了Runable 接口 为什么还要有Callable接口

1实现Runnable接口的方式中run方法没有返回值同时没有声明异常
*2.实现Callable接口的方式有返回值和异常声明,哪里开启线程就可以在哪里获取到线程的返回值

package thread;

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

public class ThreadDemo04 {
         
	public static void main(String[] args) {
		CalThread calThread = new CalThread(1,100);
		FutureTask<Integer> futureTask = new FutureTask<>(calThread);
		Thread thread = new Thread(futureTask);
		thread.start();
		
		try {
			//获取返回值
			Integer integer = futureTask.get();
			System.out.println(integer);
		} catch (InterruptedException | ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

class CalThread implements Callable<Integer>{
    private int m;
    private int n;
      	
	public CalThread() {
		super();
	}

	public CalThread(int m, int n) {
		super();
		this.m = m;
		this.n = n;
	}

	@Override
	public Integer call() throws Exception {
		int sum =0;
		for (int i = m; i <= n; i++) {
			System.out.println("CalThread:"+i);
			sum += i;
		}
		return sum;
	}

	public int getM() {
		return m;
	}

	public void setM(int m) {
		this.m = m;
	}

	public int getN() {
		return n;
	}

	public void setN(int n) {
		this.n = n;
	}
	
	
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值