2021.10.28 晴
文件复制:
一共三步
package study;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Part01_copy_01 {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(""));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(""));) {
byte bytes[] = new byte[1314811];
int count = 0;
while ((count = bis.read(bytes)) != -1) {
bos.write(bytes, 0, count);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package study;
import java.io.File;
public class Part02_copy_02 {
public static void main(String[] args) {
File file = new File("");
m1(file);
}
public static void m1(File file) {
// 判断是否是文件
if (file.isFile()) {
System.out.println(file.getAbsolutePath());
} else {
// 这里说明是文件夹
// 获取所有的子文件
File[] files = file.listFiles();
// 遍历每个子文件,判断是不是文件
for (File subFile : files) {
m1(subFile);
}
}
}
}
package study;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Part03_copy_03 {
public static void main(String[] args) {
File file = new File("");
m1(file);
}
public static void m1(File file) {
// 判断是否是文件
if (file.isFile()) {
// 获取该文件的全路径
String filePath = file.getAbsolutePath();
// 改成我们即将复制到的路径
String newFilePath = "F:\test" + filePath.substring(2);
// 获取目标文件对象
File newFile = new File(newFilePath);
// 获取父文件对象
File parentFile = newFile.getParentFile();
// 判断目标目录是否存在,不存在就创建
if (!parentFile.exists()) {
parentFile.mkdirs();
}
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(filePath));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFilePath));) {
byte bytes[] = new byte[1314811];
int count = 0;
while ((count = bis.read(bytes)) != -1) {
bos.write(bytes, 0, count);
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 这里说明是文件夹
// 获取所有的子文件
File[] files = file.listFiles();
//如果该文件夹是个空文件夹,直接创建该文件夹即可
if(files.length==0){
file.mkdirs();
}
// 遍历每个子文件,判断是不是文件
for (File subFile : files) {
m1(subFile);
}
}
}
}
多线程:
程序(program)是为完成特定任务、用某种语言编写的一组指令的集合。即指一 段静态的代码,静态对象
进程(process)是程序的一次执行过程,或是正在运行的一个程序。是一个动态
的过程:有它自身的产生、存在和消亡的过程。——生命周期
如:运行中的QQ,运行中的MP3播放器
程序是静态的,进程是动态的
进程作为资源分配的单位,系统在运行时会为每个进程分配不同的内存区域
线程(thread),进程可进一步细化为线程,是一个程序内部的一条执行路径
若一个进程同一时间并行执行多个线程,就是支持多线程的
线程作为调度和执行的单位,每个线程拥有独立的运行栈和程序计数器(pc),线程切换的开销小
单核CPU和多核CPU的概念:
单核CPU,其实是一种假的多线程,因为在一个时间单元内,也只能执行一个线程 的任务。例如:虽然有多车道,但是收费站只有一个工作人员在收费,只有收了费 才能通过,那么CPU就好比收费人员。如果有某个人不想交钱,那么收费人员可以 把他“挂起”(晾着他,等他想通了,准备好了钱,再去收费)。但是因为CPU时 间单元特别短,因此感觉不出来
如果是多核的话,才能更好的发挥多线程的效率。(现在的服务器都是多核的)
一个Java应用程序java.exe,其实至少有三个线程:main()主线程,gc() 垃圾回收线程,异常处理线程。当然如果发生异常,会影响主线程
并行:多个CPU同时执行多个任务。比如:多个人同时做不同的事。
并发:一个CPU(采用时间片)同时执行多个任务。比如:秒杀、多个人做同一件事
背景:以单核CPU为例,只使用单个线程先后完成多个任务(调用多个方法),肯定比用多个线程来完成用的时间更短,为何仍需多线程呢?
多线程程序的优点:
- 提高应用程序的响应。对图形化界面更有意义,可增强用户体验。
- 提高计算机系统CPU的利用率
- 改善程序结构。将既长又复杂的进程分为多个线程,独立运行,利于理解和
修改
何事需要多线程?
程序需要同时执行两个或多个任务。
程序需要实现一些需要等待的任务时,如用户输入、文件读写操作、网络操作、搜索等
需要一些后台运行的程序时
package study;
public class Part04_Create {
public static void main(String[] args) {
// 创建线程类对象 继承方式
Thread t1 = new Processor();
// 创建线程类对象 实现方式
Thread t2 = new Thread(new Processor_01());
// 启动 该线程 会自动调用run方法
t1.start();
t2.start();
// 但是如果我们手动调用run方法,只是方法调用而已,并不是开启新线程
// t1.run();
System.out.println("=============");
System.out.println("=============");
System.out.println("=============");
}
}
class Processor extends Thread {
@Override
public void run() {
for (int i = 5; i >= 0; i--) {
System.out.println("t1 : " + i);
}
}
}
class Processor_01 implements Runnable {
@Override
public void run() {
for (int i = 5; i >= 0; i--) {
System.out.println("t2 : " + i);
}
}
}
package study;
public class Part05_Method {
public static void main(String[] args) {
Thread t1 = new Thread(new Processer());
// 设置优先级
t1.setPriority(10);
// 设置名字
t1.setName("t1");
t1.start();
for (int i = 0; i < 10; i++) {
try {
t1.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// currentThread 获取当前线程对象
// getName : 获取当前线程对象名字
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
class Processer implements Runnable {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// currentThread 获取当前线程对象
// getName : 获取当前线程对象名字
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
package study;
public class Part06_Interrupt {
public static void main(String[] args) {
Thread t1 = new Processer_02();
t1.start();
System.out.println("开始执行....");
try {
Thread.sleep(5000);
// 唤醒t1线程
t1.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Processer_02 extends Thread {
@Override
public void run() {
try {
Thread.sleep(999999999L);
System.out.println("睡醒了");
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("被吵醒了");
}
}
}
package study;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Part07_Stop {
public static void main(String[] args) {
// 创建实现类对象
Processor_03 p = new Processor_03();
Thread t1 = new Thread(p);
t1.start();
System.out.println("main结束");
try {
Thread.sleep(3000);
// 3秒之后 强制结束t1线程
// 不推荐使用,已经过时,容易导致死锁
// t1.stop();
p.isStop = true;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Processor_03 implements Runnable {
// 标识符 : 是否停止线程
boolean isStop = false;
@Override
public void run() {
// 格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
// 死循环
for (int i = 0; true; i++) {
// 判断是否终止线程
if (isStop) {
return;
}
try {
Thread.sleep(1000);
// // 获取当前系统时间
Date date = new Date();
System.out.println(sdf.format(date));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程生命周期:
DK中用Thread.State类定义了线程的几种状态
要想实现多线程,必须在主线程中创建新的线程对象。Java语言使用Thread类 及其子类的对象来表示线程,在它的一个完整的生命周期中通常要经历如下的五 种状态:
新建: 当一个Thread类或其子类的对象被声明并创建时,新生的线程对象处于新建 状态
就绪:处于新建状态的线程被start()后,将进入线程队列等待CPU时间片,此时它已具备了运行的条件,只是没分配到CPU资源
运行:当就绪的线程被调度并获得CPU资源时,便进入运行状态, run()方法定义了线 程的操作和功能
阻塞:在某种特殊情况下,被人为挂起或执行输入输出操作时,让出 CPU 并临时中止自己的执行,进入阻塞状态
死亡:线程完成了它的全部工作或线程被提前强制性地中止或出现异常导致结束



