进程和线程
进程和线程:
进程:正在执行的程序。
线程:一条独立的执行路径。
一个进程可以只有一条线程,也可以有多条线程。
多线程
Java中至少有两个线程:主线程和垃圾回收线程。
开启多线程虽然降低了效率,但是提高了cpu的使用率,合理利用cpu的使用率。
多线程:具有完成特定功能的执行路径,是CPU最先执行单位。
CPU在某个时间刻度上只能够执行一条线程的一条原子性语句。
CPU执行原理:
1.真实环境下,CPU能够同时执行多个程序,本质只是在同一个时间刻度上执行一条线程的一条原子性语句。
只不过CPU切换执行速度非常快,我们无法察觉以为是同时执行。
2.并发和并行:
并发:在同一个时间段同时执行。
并行:在同一时间刻度上同时执行。
3.同步和异步:
同步:并发情况下会出现同步问题。
异步:能够同一个时间段处理多个任务。
开启多线程的好处:
1.执行某些耗时任务。
2.希望某些程序看起来像同事执行。
3.希望完成某个特定的子任务。
4.防止线程阻塞。
多线程的启动方式
方式一
方式一:继承Thread类
1.自定义类MyThread继承Thread类
2.MyThread类重写run方法。
3.创建线程对象
public class ThreadSeatwork1 {
public static void main(String[] args) {
// 創建線程對象
MyThread mt = new MyThread(new File("ThreadSeatwork1.java"), new File("thread.txt"));
mt.start();
}
}
//1.自定义类MyThread继承Thread类
class MyThread extends Thread{
private File srcFile;
private File descFile;
public MyThread() {
super();
}
public MyThread(File srcFile, File descFile) {
super();
this.srcFile = srcFile;
this.descFile = descFile;
}
// 2.MyThread类重写run方法。
@Override
public void run() {
copyFile(srcFile, descFile);
}
public void copyFile(File sreFile, File descFile) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(descFile));){
int len = 0;
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 e1) {
e1.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;
}
}
注意:
1.启动线程使用的是start()方法 而不是run()方法。
2.线程不能多次启动
方式二
方式二:实现Runnable接口
1.自定义类MyRunnable实现Runnable接口
2.重写run()方法
3.创建MyRunnable类的对象
4.创建Thread类的对象,并把步骤3创建的对象作为构造参数传递
5.启动线程
实现接口方式的好处:
可以避免由于Java单继承带来的局限性
适合多个相同程序的代码去处理同一个资源的情况,把线程同程序的代码,数据有效分离,
较好的体现了面向对象的设计思想
public class ThreadSeatwork2 {
public static void main(String[] args) {
MyRunnable mr = new MyRunnable();
Thread t = new Thread(mr);
t.start();
for (int i = 0; i < 100; i++) {
System.out.println("主线程" + i);
}
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("子线程" + i);
}
}
}
方式三
实现Callable方式开启线程
FutureTask
public class FutureTask Implements RunnableFuture {
}
public interface RunnableFuture extends Runnable, Future{
void run();
}
public interface Future {
V get() throws InterrptedException, ExecutionException;
}
实现Runnable和实现Callable接口的区别
1.有返回值
2.可以声明异常
这里的返回值和异常抛出都是给到线程的启动者
public class ThreadSeatwork3 {
public static void main(String[] args) {
FutureTask<Integer> task = new FutureTask<>(new MyCallable(1, 100));
Thread t = new Thread(task);
t.start();
for (int i = 0; i < 100; i++) {
System.out.println("main" + i);
}
try {
Integer value = task.get();
System.out.println(value);
} catch (InterruptedException | ExecutionException e) {
System.out.println("子线程抛出异常给主线程" + e);
e.printStackTrace();
}
System.out.println("over");
}
}
class MyCallable implements Callable<Integer> {
private Integer m;
private Integer n;
public MyCallable() {
super();
}
public MyCallable(Integer m, Integer n) {
super();
this.m = m;
this.n = n;
}
@Override
public Integer call() throws Exception {
int sum = 0;
for (int i = 0; i < 100; i++) {
System.out.println(i);
sum += i;
}
return sum;
}
public Integer getM() {
return m;
}
public void setM(Integer m) {
this.m = m;
}
public Integer getN() {
return n;
}
public void setN(Integer n) {
this.n = n;
}
}
方式四
方法四:匿名内部类 开启多线程
public class ThreadSeatwork4 {
public static void main(String[] args) {
new Thread();
new Thread().start();
// 方式一继承Thread开启线程
new Thread() {}.start();
new Thread() {
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("方法一继承Thread开启线程" + i);
}
};
}.start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("方式二实现Runnable开启线程" + i);
}
}
}).start();
for (int i = 0; i < 100; i++) {
System.out.println("主方法开启线程" + i);
}
// 如果一个线程既继承了Thread,同时实现了Runnable接口,那么继承Thread优先
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("方式二实现Runnable开启线程---" + i);
}
}
}) {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("方法一继承Thread开启线程---" + i);
}
super.run();
}
}.start();
}
}