合并流
1.合并流 SequenceInputStream
合并功能(可以把 多个文件合读成成一个文件)
构造方法:
参数 是迭代器 是Vector特有的
该Vector要保存的是 InputStream的子类
SequenceInputStream(Enumeration<? extends InputStream> e)
参数 传两个字节输入流
SequenceInputStream(InputStream s1, InputStream s2)
File file1 = new File("/Users/lanou/Desktop/Test/01.txt");
File file2 = new File("/Users/lanou/Desktop/Test/02.txt");
File file3 = new File("/Users/lanou/Desktop/Test/03.txt");
Vector<FileInputStream> vector = new Vector<>();
vector.add(new FileInputStream(file1));
vector.add(new FileInputStream(file1));
vector.add(new FileInputStream(file1));
Enumeration<FileInputStream> elements = vector.elements();
SequenceInputStream sis = new SequenceInputStream(elements);
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/Test/04.txt");
byte[] b = new byte[1024];
int len = 0;
while((len = sis.read(b)) != -1){
fos.write(b, 0, len);
}
sis.close();
fos.close();
进程和线程
2.进程:一个正在运行的程序(独立运行的程序)
一个进程中可以只有一个线程 单线程程序
一个进程中可以有多个线程 多线程程序(很大提升了执行效率)
线程:一个线程相当于一个cpu的执行路径 是一个独立的运行单元
代码如何开启线程:JVM调用main方法--->找操作系统--->开启一个名字是main的执行路径
main就是一个线程 叫主线程
实现多线程的两种方式:
第一种:
1.创建类继承Thread类
2.重写父类中的run 方法
class SubThread extends Thread{
public SubThread(){
}
public SubThread(String name){
super(name);
}
@Override
public void run(){
for(int i = 0; i < 50; i++){
System.out.println(getName() + " run方法 " + i);
}
}
}
public static void main(String[] args) {
SubThread subThread = new SubThread("线程1");
subThread.start();
Thread currentThread = Thread.currentThread();
System.out.println(currentThread.getName);
for (int i = 0; i < 50; i++) {
System.out.println("main方法" + i);
}
}
第二种:接口实现方式来创建对象
class RunnableImpl implements Runnable{
@Override
public void run() {
for (int i = 0; i < 50; i++) {
System.out.println(Thread.currentThread().getName() + "--" + i);
}
}
public static void main(String[] args) {
RunnableImpl rImpl = new RunnableImpl();
Thread thread = new Thread(rImpl);
thread.start();
}
Thread中的set/get方法
3.创建一个Thread子类 要求: 成员变量 String name set/get方法 重写run方法 要有(带名字)构造方法 无参
public static void main(String[] args) {
}
}
class NameThread extends Thread{
private String name;
public NameThread() {
}
public NameThread(String name, String MyName) {
super(name);
this.name = MyName;
}
public String getMyName() {
return name;
}
public void setMyName(String name) {
this.name = name;
}