forked from Somersames/JavaBasis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileThread.java
More file actions
82 lines (74 loc) · 2.55 KB
/
FileThread.java
File metadata and controls
82 lines (74 loc) · 2.55 KB
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
73
74
75
76
77
78
79
80
81
82
package Thread;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by szh on 2017/3/27.
* 测试在多线程的情况下普通IO的读写情况
* 在未加入flush方法的时候一直写不到10000,需要主要每次写入
*/
public class FileThread {
public static void main(String args[]) throws InterruptedException, IOException {
FileInOut fileInOut1 =new FileInOut("D:\\disk\\qqq.txt","第一个");
FileInOut fileInOut2 =new FileInOut("D:\\disk\\qqq.txt","第二个");
new Thread(fileInOut1).start();
new Thread(fileInOut2).start();
Thread.sleep(5000);
System.out.println("已经推出");
// wr();
}
public static void wr() throws IOException {
int t = 0;
File file = new File("D:\\disk\\qqq.txt");
Writer writer = new FileWriter(file);
while (t < 10000) {
writer.write(t + Thread.currentThread().getName());
writer.flush();
t++;
}
writer.close();
}
}
class FileInOut extends Thread {
String path;
String content;
public FileInOut(String path, String content) {
this.path = path;
this.content = content;
}
@Override
public void run() {
try {
synchronized (this) {
reou(this.path, this.content);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void reou(String path, String content) throws IOException, InterruptedException {
Writer writer = new FileWriter(new File(path),true);
// String s1="he";
// writer.write(s1+content);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(simpleDateFormat.format(new Date()) + "-----------" + Thread.currentThread().getName());
int i = 0;
StringBuffer sb = new StringBuffer();
while (i < 10000) {
synchronized (writer) {
sb.append("0");
writer.write(i + Thread.currentThread().getName()+"\r\n");
writer.flush();
i++;
}
}
System.out.println(i);
System.out.println(sb.toString().length() + "sb的值");
System.out.println(simpleDateFormat.format(new Date()) + "-----------" + Thread.currentThread().getName());
// String s2="llo"+content;
// writer.write(s2);
// writer.close();
}
}