实现方式一 继承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;
}
}