学习笔记:Java基础查缺补漏(三)

来源于慕课网Java零基础入门

好多基础的东西都忘记了,补起来!

线程

创建线程的几类方法

  • 继承Thread类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void func1() {
class MyThread extends Thread {
MyThread(String id) {
super(id);
}
@Override
public void run() {
int i = 0;
while ( i < 20 ) {
System.out.println("t" + this.getName() + " " + i++);
}
}
}

new MyThread("1").start();
new MyThread("2").start();
}
  • 实现Runnable接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void func2() {
class MyThread implements Runnable {

@Override
public void run() {
int i = 0;
while ( i < 20 ) {
System.out.println("t" + Thread.currentThread().getName() + " " + i++);
}
}
}

new Thread(new MyThread()).start();
new Thread(new MyThread()).start();
}
  • 实现Callable接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static void func3() {
FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
return "第三种创建方式";
}
});

new Thread(futureTask).start();

try {
String output = futureTask.get();
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}

Sleep方法使用

注意,因为线程调度的一些因素,从停止到开始的时间间隔可能会大于设定的时长

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static void func4(){
new Thread(new Runnable() {
@Override
public void run() {
int i = 0;
while ( i < 50 ) {
System.out.println("t" + Thread.currentThread().getName() + " " + i++);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}

其他

  • 调用join()/join(millionsecond)的线程优先执行

    • 可以在主线程中调用子线程的这个方法来使其等到子线程运行结束
  • 调用方法 getPriority, setPriority 对其优先级进行操作,优先级从小到大1-10,默认为5

  • 线程间通信

    • wait() 阻塞
    • notify() 唤醒
    • notifyAll()

字节流 对二进制进行操作

  • InputStream 输入

  • OutputStream 输出

  • FileInputStream
    • public int read() 读一个字节
    • public inr read(byte[] b) 读入一个字节数组中 -1表示已经读到了末尾
    • public int read(byte[] b, int off, int len)
    • public void close() 关闭
1
2
3
int n;
while ((n = fis.read()) != -1) {...}
fis.close();
  • FileOutputStream

    • FileOutputStream(File, ”是否追加“)
    • public void write(int b) 写一个字节
    • public void write(byte[] b) 写一个字节数组
    • public void write(byte[] b, int off, int length)
    • public void close()
  • 对文件的拷贝

1
2
3
4
byte[] b = new byte[1024];
while((n = fis.read(b))!=1){
fos.write(b, 0, n);
}
  • BufferedInputStream

  • BufferedOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

/**
* 项目名:learning
* 包名: com.example.io
* 描述: 三种方法实现对文件的拷贝
*
* @author schwarzeni
* @date 2018/12/29 8:29 AM
*/
public class Copy {

private static File srcFile = new File("image1.png");
private static File destFile = new File("image1-copy.png");
private static int bufferSize = 1024;

private static void copy1() throws FileNotFoundException, IOException {
InputStream inputStream = new FileInputStream(srcFile);
OutputStream outputStream = new FileOutputStream(destFile);

byte[] buffer = new byte[bufferSize];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}

inputStream.close();
outputStream.close();
}

public static void copy2() throws IOException {
InputStream inputStream = new FileInputStream(srcFile);
OutputStream outputStream = new FileOutputStream(destFile);

BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);

byte[] buffer = new byte[bufferSize];
int len;
while ((len = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}

bufferedInputStream.close();
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
outputStream.close();
}

public static void copy3() {
FileChannel srcFileChannel;
FileChannel destFileChannel;
try {
RandomAccessFile srcFile = new RandomAccessFile(this.srcFilePath, "rw");
RandomAccessFile destFile = new RandomAccessFile(this.destFilePath, "rw");
srcFile.seek(idx);
destFile.seek(idx);
srcFileChannel = srcFile.getChannel();
destFileChannel = destFile.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(this.bufferSize);
byteBuffer.flip();

srcFileChannel.transferTo(this.idx, this.length, destFileChannel);
srcFileChannel.close();
destFileChannel.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
}

字符流

  • Reader

  • Writer

  • InputStreamReader(FileInputStream, “utf-8”)

  • OutputStreamWriter(FileOutputStrea, “GBK”)

    • getEncodings

对象序列化

  • Serializable 接口
  • ObjectInputStream(Inputstream)
  • ObjectOutputStream()