-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.java
More file actions
60 lines (55 loc) · 1.34 KB
/
sync.java
File metadata and controls
60 lines (55 loc) · 1.34 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
package Synchronization;
public class sync {
public static void main(String[] args){
Memory mem = new Memory(16);
DownLoad down = new DownLoad(mem);
Play play = new Play(mem);
down.start();
play.start();
}
}
class Memory{
int[] buffer;
int size;
int progress;
Memory(int size){
this.size = size;
this.buffer = new int[size];
this.progress = 0;
}
}
class DownLoad extends Thread{
Memory mem;
DownLoad(Memory mem){
this.mem = mem;
}
public void run(){
for(int off=0; off < mem.size; off += 2){
for(int chunk = 0; chunk < 2; chunk++){
mem.buffer[off+chunk] = off+chunk;
mem.progress = off + chunk + 1;
try{
Thread.sleep(200);
} catch (InterruptedException e) {}
}
}
}
}
class Play extends Thread{
Memory mem;
Play(Memory mem){
this.mem = mem;
}
public void run(){
for(;;){
for(int off = 0; off < mem.progress; off++){
System.out.print(mem.buffer[off] + " ");
}
System.out.println();
if(mem.progress == mem.size) break;
try{
Thread.sleep(500);
} catch(InterruptedException e) {}
}
}
}