-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriterReader.java
More file actions
46 lines (42 loc) · 1.32 KB
/
Copy pathWriterReader.java
File metadata and controls
46 lines (42 loc) · 1.32 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
package thread.demo2;
/**
* @Author: wei1
* @Date: Create in 2019/1/1 10:50
* @Description:
*/
public class WriterReader {
private Object lock;
public WriterReader() {
lock = this;
}
public void write() {
synchronized (lock) {
long startTime = System.currentTimeMillis();
System.out.println("开始往这个buff写入数据…");
for (; ; )// 模拟要处理很长时间
{
if (System.currentTimeMillis()
- startTime > Integer.MAX_VALUE) {
System.out.println("终于写完了");
break;
}
if (Thread.currentThread().isInterrupted()) {
System.out.println("被中断");
System.out.println(Thread.interrupted());
System.out.println(Thread.interrupted());
System.out.println(Thread.interrupted());
break;
}
System.out.println("写.........");
}
}
}
public void read() {
synchronized (lock) {
System.out.println("从这个buff读数据");
if (Thread.currentThread().isInterrupted()) {
System.out.println("read被中断");
}
}
}
}